简体   繁体   中英

iOS JSON Parse into NSDictionary and then NSArray with SBJson

It should be so simple but I cannot get it work.

Json response is ([{"id":"1", "x":"1", "y":"2"},{"id":2, "x":"2", "y":"4"}])

NSString *response = [request responseString];
//response is ([{"id":"1", "x":"1", "y":"2"},{"id":2, "x":"2", "y":"4"}])

SBJSON *parser = [[[SBJSON alloc] init] autorelease];

NSDictionary *jsonObject = [parser objectWithString:response error:NULL];
// jsonObject doesn't have any value here..Am I doing something wrong?

NSMutableArray Conversion = [jsonObject valueForKey:NULL];
//Even if I get the value of jsonObject. I don't know what to put for valueForKey here

Conversion shoud have two NSObjects..and each of them should have like

id:1 x:1 y:2

and

id:2 x:2 y:4

Your JSON parser will produce an NSArray from your response string, not an NSDictionary. Note that JSON parsers, including SBJSON, will return either an array object or a dictionary object, depending on the contents of the json that is being parsed.

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

You can then access the individual items in your array (the array elements will be of type NSDictionary) and use valueForKey: to get the properties of each item.

NSDictionary *firstItem = [jsonObject objectAtIndex:0];
NSString *theID = [firstItem objectForKey:@"id"];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM