Pergunta

I'm currently experiencing a weird issue when it comes to parsing some JSON when using the SBJsonParser.

Now, the JSON that I am parsing is as follows.

[
   {
      "Company":{
         "id":"1",
         "company_name":null
      },
      "relations":{
         "store":[
            {
               "id":"1",
               "restaurant_name":"Dubai",
               "brief_description":null
            },
            {
               "id":"2",
               "restaurant_name":"Test2",
               "brief_description":null
            }
         ]
      }
   }
]

I can easily create an NSDictionary and fill it with the correct information for the Company node(?).

But my issue arises when it comes to the relations and the store nodes.

 NSDictionary *relations = [object valueForKey:@"relations"];
 NSArray *multipleStores = [relations valueForKey:@"store"];
 NSLog(@"relations: %@", relations);

 for (NSDictionary *singleStore in multipleStores){

        NSLog(@"Single Store: %@", singleStore);

        [company grabStoreInformation:singleStore];

 }

Here is what the NSLog's above return.

 relations: (
    {
    store =         (
                    {
            "brief_description" = "<null>";
            id = 1;
            "restaurant_name" = Dubai;
        },
                    {
            "brief_description" = "<null>";
            id = 2;
            "restaurant_name" = Test2;
        }
    );
 }
)

Now this would be fine if it weren't for what was happening in the NSLog. It seems that singleStore isn't actually getting the individual store nodes, but adding both of the store nodes together.

Single Store: (
    {
    "brief_description" = "<null>";
    id = 1;
    "restaurant_name" = Dubai;
    },
    {
    "brief_description" = "<null>";
    id = 2;
    "restaurant_name" = Test2;
    }
)

The issue is, I need each store node to be added into an NSMutableArray. So the NSDictionary will then be added into an NSMutableArray and then accessible elsewhere (for a UITableView data source).

Any help would be greatly appreciated in getting the store nodes separate.

EDIT As asked for, entire code for parsing:

        SBJsonParser *parser = [[SBJsonParser alloc] init];

        // parse the JSON string into an object - assuming [response asString] is a NSString of JSON data
        NSDictionary *object = [parser objectWithString:[response asString] error:nil];

        [parser release];

        NSDictionary *companyDetails = [object valueForKey:@"Company"];

        MACompany *company = [MACompany sharedMACompany];
        [company initWithDetails:companyDetails];

        NSDictionary *relations = [object valueForKey:@"relations"];
        NSArray *multipleStores = [relations valueForKey:@"store"];

        NSLog(@"relations: %@", relations);

        for (NSDictionary *singleStore in multipleStores){

            NSLog(@"Single Store: %@", singleStore);

            [company grabStoreInformation:singleStore];

        }

As you can see I rely on a singleton class to copy elements of the JSON. This, I don't think, is relevant to what I am trying to achieve when it comes to working out how to split the single store dictionary.

Foi útil?

Solução

Firstly, note that your top-level element is an array, not an object. Hence:

NSDictionary *object = [parser objectWithString:[response asString] error:nil];

should be:

NSArray *object = [parser objectWithString:[response asString] error:nil];

and I’d use companies instead of object as the variable name.

Secondly, you must be careful when using -valueForKey:. When sent to an array, it returns another array containing the values corresponding to the key argument. Unless you are familiar with Key-Value Coding, use the canonical method in NSDictionary to return the object associated to a given key: -objectForKey:.

Had you used -objectForKey: instead of -valueForKey:, you would have noticed this structural error when running your program.

With those two points in mind, you can navigate your JSON data as follows:

NSArray *companies = [parser objectWithString:[response asString] error:nil];
for (NSDictionary *company in companies) {
    NSDictionary *companyDetails = [company objectForKey:@"Company"];
    NSDictionary *relations = [company objectForKey:@"relations"];
    NSArray *stores = [relations objectForKey:@"store"];

    for (NSDictionary *store in stores) {
        NSString *restaurantName = [store objectForKey:@"restaurant_name"];
        …
    }

I recommend reading this other question on Stack Overflow: Difference valueforKey objectForKey and the Key-Value Coding Programming Guide before using -valueForKey:.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top