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.
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.