2013-04-26 34 views
0

这是我的JSON。我想解析,以便当我将它推入我的数组时,我的JSON中的第一个对象首先出现。但现在它似乎随机解析?有时按照这个顺序:1,5,3,2,4有时候是2,3,1,5,4。有谁知道我能做些什么来让他们按正确的顺序?SBJSON每次都按随机顺序解析?

解析
{ 
    "1": 
    [ 
     { 
      "film": 
      [ 
       { 
        "title":"film1", 
        "description": "desc1" 
       }, 
       { 
        "title": "film2", 
        "description": "desc2" 
       } 
      ], 
      "tv": 
      [ 
        { 
         "title": "tv", 
         "description": "desc1" 
        }, 
        { 
         "title": "tv2", 
         "description": "desc2" 
        } 
       ] 
     } 
    ], 
    "2": 
    [ 
     { 
      "category2": 
      [ 
       { 
        "title":"item1", 
        "description": "desc1" 
       }, 
       { 
        "title": "item2", 
        "description": "desc2" 
       } 
      ] 
     } 
    ], 
    "3": 
    [ 
    { 
    "category2": 
    [ 
     { 
     "title":"item1", 
     "description": "desc1" 
     }, 
     { 
     "title": "item2", 
     "description": "desc2" 
     } 
     ] 
    } 
    ], 
    "4": 
    [ 
    { 
    "category2": 
    [ 
     { 
     "title":"item1", 
     "description": "desc1" 
     }, 
     { 
     "title": "item2", 
     "description": "desc2" 
     } 
     ] 
    } 
    ], 
    "5": 
    [ 
    { 
    "category2": 
    [ 
     { 
     "title":"item1", 
     "description": "desc1" 
     }, 
     { 
     "title": "item2", 
     "description": "desc2" 
     } 
     ] 
    } 
    ] 

} 

代码:

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"json"]; 

NSString *contents = [NSString stringWithContentsOfFile: filePath encoding: NSUTF8StringEncoding error: nil]; 
SBJsonParser *jsonParser = [[SBJsonParser alloc] init]; 

NSMutableDictionary *json = [jsonParser objectWithString: contents]; 

tabs = [[NSMutableArray alloc] init]; 

jsonParser = nil; 



for (NSString *tab in json) 
{ 
    Tab *tabObj = [[Tab alloc] init]; 
    tabObj.title = tab; 

    NSLog(@"%@", tabObj.title); 

    NSDictionary *categoryDict = [[json valueForKey: tabObj.title] objectAtIndex: 0]; 
    for (NSString *key in categoryDict) 
    { 

     Category *catObj = [[Category alloc] init]; 
     catObj.name = key; 


     NSArray *items = [categoryDict objectForKey:key]; 
     // you should add error checking to make sure this is actually an NSArray 

     for (NSDictionary *dict in items) 
     { 
      Item *item = [[Item alloc] init]; 
      item.title = [dict objectForKey: @"title"]; 
      item.desc = [dict objectForKey: @"description"]; 

      [catObj.items addObject: item]; 

     NSLog(@"----%@", catObj.items); 

     } 

     [tabObj.categories addObject: catObj]; 

     NSLog(@"%@", tabObj.categories); 

    } 

    [tabs addObject: tabObj]; 


} 

回答

1

NSMutableDictionary是根本无法订购。当您请求密钥时,它们将以不确定的顺序返回给您。 JSON文本有一个顺序的事实与此无关,因为元数据在字典中返回时解析过程中会丢失。

以正确的顺序获取它们取决于正确的顺序。如果您可以从字典中获取密钥并对其进行排序,那么只需使用compare:即可。如果您确实需要原始JSON中的订单,那么您需要深入分析操作并使用SBJSON类提供的委派选项添加额外信息。

0

如果你控制的输入源,可以保持一个有序列表和控制输入这样:

-(NSArray *)orderedPartsList { 
    return @[@"tubes", @"dynamic", @"tracks", @"passives",@"actives",@"walls", @"curvedWalls", @"glassCovers", @"enemies"]; 
} 

NSString *key = [self orderedPartsList][indexPath.section]; 
id the orderedObject = [someCurrentDictionary objectForKey:key] 

如果从别人的Web服务器,那么你没有太多的选择,因为字典(哈希)是无序的。