Reputation: 891
Trying to parse this json and cannot seem to figure it out.
{ description = "Description Variant 1 "; id = 4; price = "25.0"; }, { description = "Variant 2 Description "; id = 5; price = "50.0"; }, { description = "Variant 3 Description"; id = 6; price = "75.0"; }
Here is my code, but I get a SigAbt on the NSLog:
- (NSMutableArray *) getVariants:(NSString *)variantJson
{
NSMutableArray *variants = [[NSMutableArray alloc] init];
NSLog(@"Variant JSON: %@", variantJson);
NSArray *vars = [variantJson valueForKeyPath:@"variants"];
for (id var in vars)
{
NSLog(@"description: %@",[var objectForKey:@"description"]);
}
return variants;
}
The json coming in to variable: variantJson is the above posted JSON.
Upvotes: 1
Views: 1329
Reputation: 11552
You have no code for parsing the JSON there. Objective-C and Cocoa have not built-in mechanism for automagically parsing JSON string into objects and dictionaries and valueForKeyPath
is for getting property value (within hierarchy) of KVC-compliant objective-c classes.
In order to get nested NSDictionary
'ies and NSArray
's you need to employ some third party library or write your own code.
Take a look at the list of libraries at JSON page.
Upvotes: 1
Reputation: 2342
iOS doesn't parse JSON this transparently; you need to run your string through an actual JSON parser library, like SBJson. (BSD-licensed) Or you can use the built-in NSJSONSerialization if you're targeting OS 5 or later.
Upvotes: 2