I’m developing an iOS application with latest SDK and XCode 4.2.
To parse JSON response, I’m using this JSON parser: https://github.com/stig/json-framework/
I receive this JSON string from web service (this is console log):
2012-02-16 08:21:59.525 JReader[558:207] {"rules": [
{ "id_categoria": "3","categoria": "cat03" },{ "id_categoria": "2","categoria": "cat02" }
]
}
JSON parser parses to a NSArray with these two elements (this is console log):
2012-02-16 08:22:04.910 JReader[558:207] {
categoria = "cat03";
"id_categoria" = 3;
}
2012-02-16 08:22:04.911 JReader[558:207] {
categoria = "cat02";
"id_categoria" = 2;
}
Objective-C code to parse is:
- (IBAction)go:(id)sender
{
parser = [[SBJsonParser alloc] init];
NSURLRequest* request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://XXX"]];
NSData* response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
// parse the JSON response into an object
// Here we're using NSArray since we're parsing an array of JSON status objects
NSLog(@"%@", json_string);
NSDictionary* datos = [parser objectWithString:json_string error:nil];
NSArray* data = [datos objectForKey:@"rules"];
for (int i = 0; i < data.count; i++)
{
NSLog(@"%@", [data objectAtIndex:i]);
}
}
I expect a NSDictionary with these pairs:
key value
============ ============
categoria cat03
id_categoria 3
MY QUESTIONS:
-
Is there any error on json string?
-
Why is it parsing this way?
It is my first time parsing JSON, so I don’t know if it is parsing well.
There is nothing wrong with that. It’s an array of dictionaries inside a dictionary with the key
rules. That’s exactly what you got.