0

これが私のJSONです。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]

他の誰かの Web サーバーからの場合、辞書 (ハッシュ) は順不同であるため、選択の余地はあまりありません。

于 2015-02-28T04:01:22.687 に答える