2011-10-28 52 views
0

当我使用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的元素。我认为,这与我试图解决单个商店字典拆分问题时所达到的目标有关。

+0

偏离主题,但如果您的目标是OS5,则可以使用内置的JSON解析器 – Devraj

+0

我们也支持iOS4。在我看来,也可以使用相同的流程来处理iOS。 –

+0

@SebastienPeek足够:) – Devraj

回答

1

首先,请注意,您的顶级元素是一个数组,而不是一个对象。因此:

NSDictionary *object = [parser objectWithString:[response asString] error:nil]; 

应该是:

NSArray *object = [parser objectWithString:[response asString] error:nil]; 

,我会用companies代替object作为变量名。

其次,使用-valueForKey:时一定要小心。当发送到一个数组时,它返回另一个数组,其中包含对应于键参数的值。除非您熟悉Key-Value编码,否则请使用NSDictionary中的规范方法返回与给定键关联的对象:-objectForKey:

如果您使用-objectForKey:而不是-valueForKey:,那么在运行程序时您会注意到这种结构错误。

考虑到这两点,你可以浏览你的JSON数据如下:

NSArray *companies = [parser objectWithString:[response asString] error:nil]; 
for (NSDictionary *company in companies) { 
    NSDictionary *companyDetails = [company objectForKey:@"Company"]; 
    NSDictionary *relations = [company objectForKey:@"relations"]; 
    NSArray *stores = [relations objectForKey:@"store"]; 

    for (NSDictionary *store in stores) { 
     NSString *restaurantName = [store objectForKey:@"restaurant_name"]; 
     … 
    } 

我建议您阅读堆栈溢出这个其他问题:使用-valueForKey:之前Difference valueforKey objectForKeyKey-Value Coding Programming Guide

+0

完美的作品。我唯一的问题是,比如说我想将NSDictionary *存储添加到NSMutableArray中,这样我就可以在以后将它拉出来,可以只做一个[NSMutableArray addObject:store]? –

+1

@Sebastien当然。另一种选择是创建'stores'数组的可变副本,例如'NSMutableArray * storesArray = [存储mutableCopy]'。 – 2011-10-28 04:03:06

+0

谢谢,确实很有帮助! –