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.