当我使用SBJsonParser解析某些JSON时,我目前遇到了一个奇怪的问题。解析JSON - SBJsonParser问题
现在,我解析的JSON如下。
[
{
"Company":{
"id":"1",
"company_name":null
},
"relations":{
"store":[
{
"id":"1",
"restaurant_name":"Dubai",
"brief_description":null
},
{
"id":"2",
"restaurant_name":"Test2",
"brief_description":null
}
]
}
}
]
我可以轻松地创建一个NSDictionary,并为公司节点正确的信息填充(?)。
但是,当涉及到关系和店节点我的问题就出现了。
NSDictionary *relations = [object valueForKey:@"relations"];
NSArray *multipleStores = [relations valueForKey:@"store"];
NSLog(@"relations: %@", relations);
for (NSDictionary *singleStore in multipleStores){
NSLog(@"Single Store: %@", singleStore);
[company grabStoreInformation:singleStore];
}
这是NSLog上面的回报。
relations: (
{
store = (
{
"brief_description" = "<null>";
id = 1;
"restaurant_name" = Dubai;
},
{
"brief_description" = "<null>";
id = 2;
"restaurant_name" = Test2;
}
);
}
)
现在,如果不是因为什么在发生的NSLog这将是罚款。看来singleStore实际上并没有获取单独的存储节点,而是将两个存储节点添加到一起。
Single Store: (
{
"brief_description" = "<null>";
id = 1;
"restaurant_name" = Dubai;
},
{
"brief_description" = "<null>";
id = 2;
"restaurant_name" = Test2;
}
)
的问题是,我需要每家店节点添加到一个NSMutableArray。所以NSDictionary将被添加到一个NSMutableArray中,然后在别处访问(对于UITableView数据源)。
任何帮助将非常感谢让商店节点分开。
编辑 至于要求,整个代码解析:
SBJsonParser *parser = [[SBJsonParser alloc] init];
// parse the JSON string into an object - assuming [response asString] is a NSString of JSON data
NSDictionary *object = [parser objectWithString:[response asString] error:nil];
[parser release];
NSDictionary *companyDetails = [object valueForKey:@"Company"];
MACompany *company = [MACompany sharedMACompany];
[company initWithDetails:companyDetails];
NSDictionary *relations = [object valueForKey:@"relations"];
NSArray *multipleStores = [relations valueForKey:@"store"];
NSLog(@"relations: %@", relations);
for (NSDictionary *singleStore in multipleStores){
NSLog(@"Single Store: %@", singleStore);
[company grabStoreInformation:singleStore];
}
正如你可以看到我靠一个单独的类复制JSON的元素。我认为,这与我试图解决单个商店字典拆分问题时所达到的目标有关。
偏离主题,但如果您的目标是OS5,则可以使用内置的JSON解析器 – Devraj
我们也支持iOS4。在我看来,也可以使用相同的流程来处理iOS。 –
@SebastienPeek足够:) – Devraj