2013-04-26 20 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服務器,那麼你沒有太多的選擇,因爲字典(哈希)是無序的。