0

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

{
    "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];


}
4

2 回答 2

1

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

让它们以“正确”的顺序排列取决于正确的顺序是什么。如果您可以从字典中获取键并对它们进行排序,则只需使用compare:. 如果您确实需要原始 JSON 中的顺序,那么您将需要深入解析操作并使用 SBJSON 类提供的委托选项添加额外信息。

于 2013-04-26T12:51:50.800 回答
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]

如果它来自其他人的网络服务器,那么您没有太多选择,因为字典(哈希)是无序的。

于 2015-02-28T04:01:22.687 回答