0

My Json response is

[{"id":"1", "x":"1", "y":"2"},{"id":2, "x":"2", "y":"4"}]

And I did

 NSString *response = [request responseString];
 SBJSON *parser = [[SBJSON alloc] init];
 NSArray *jsonObject = [parser objectWithString:response error:nil];

At this point I believe that jsonObject is a array that has two NSDictionary.. How can I make jsonObject that has two NSArray instead of NSDictionary? What is the best way to do so? I think that I need to convert nested NSDictionary to NSArray?

rid
  • 61,078
  • 31
  • 152
  • 193
MomentH
  • 1,979
  • 5
  • 22
  • 24
  • There's no straightforward conversion from dictionary to array - they're different data structures with different meaning; you're going to have to define how you want the conversion to happen, and it's likely to involve you adding / removing some meaning (e.g. an ordering) – Kristian Glass Apr 23 '12 at 23:21

1 Answers1

1

I would use the NSJSONSerialization class now in the Foundation framework.

With the JSONObjectWithData method, it will return the type of container you want the data stored in at the top level. For example:

NSError *e;

NSArray *jsonObject = [NSJSONSerialization JSONObjectWithData:data options:0 error:&e]; 

for (NSDictionary *dict in jsonObject) {

    NSLog(@"json data:\n %@", dict); 

    // do stuff

}

Alternately, you could return a mutable container, such as:

NSMutableDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:&e];

Here is the Apple documentation:

NSJSONSerialization

Chris Livdahl
  • 4,662
  • 2
  • 38
  • 33