2

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

Hailei
  • 42,163
  • 6
  • 44
  • 69
MomentH
  • 1,979
  • 5
  • 22
  • 24

1 Answers1

6

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"];
jonkroll
  • 15,682
  • 4
  • 50
  • 43
  • Tried and got invalid CFArrayRef for jsonObject..It seems that jsonObject is not getting anything.. I checked with breakpoint and able to see parser got json value.. – MomentH Apr 23 '12 at 22:23
  • I got it.. Your answer was perfect :) but Json return I got has ( and ) so parser cannot process it.. I have removed with response = [response stringByReplacingOccurrencesOfString:@"(" withString:@""]; and response = [response stringByReplacingOccurrencesOfString:@")" withString:@""]; ** and then it's working perfect.. thanks.. :) Is there some easy way to remove '(' and ')' at a time? – MomentH Apr 23 '12 at 22:43