objective-csbjson

SBJSON parsing in random order every time?


Here's my JSON. I want to parse so that the first object in my JSON comes first when I push it into my array. But now it seems to parse random? Sometimes it's in this order: 1,5,3,2,4 sometimes it's 2,3,1,5,4. Does anyone know what I can do to get them in the right order?

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

}

Code for parsing:

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


}

Solution

  • NSMutableDictionary is simply not ordered. When you request the keys they are returned to you in a non-deterministic order. The fact that the JSON text has an order is beside the point as that meta-data is lost during the parse when the data is returned in a dictionary.

    Getting them in the 'right' order depends what the right order is. If you can get the keys from the dictionary and sort them then just use compare:. If you truly need the order as in the original JSON then you will need to dig into the parse operation and add extra information by using the delegation options offered by SBJSON classes.