1

I've got an OData service that can return JSON values for an object collection. I'd like to point an iPhone app at a collection of JSON objects off of that service (results shown below with one sample record).

How can I parse these nested values? When I convert the response string to JSON values, it only grabs "d" (my code for that is also below).

{
"d" : {
"results": [
{
"__metadata": {
"uri": "http://someserver/service.svc/collection(1234L)", "type": "My.Namespace.Type"
}, "Property1": "value1", "Property2": 7274, "Collection1": {
"__deferred": {
"Property3": "http://someserver/service.svc/collection(1234L)/Images"
}
}
},
...

Sample objective C code:

- (void)requestFinished:(ASIHTTPRequest *)request
{
    if (request.responseStatusCode == 200)
    {
        NSString *responseString = [request responseString];

        textView.text = responseString;

        NSDictionary *responseDict = [responseString JSONValue];
        NSArray *keys = [responseDict allKeys];

        [self printArray:keys]; // This prints "d"
        ...
     }
}
Brandon Linton
  • 4,373
  • 5
  • 42
  • 63

4 Answers4

2

It seems that you have a root key "d" and all the keys are in fact subkeys, try

NSDictionary *responseDict = [[responseString JSONValue] objectForKey:@"d"];

or even

NSArray *responseArray = [[[responseString JSONValue] objectForKey:@"d"] objectForKey:@"results"];

it's an alternance of NSArray and NSDictionnary :

NSDictionary *responseDict = [[responseString JSONValue] valueForKey:@"d"];
NSArray *responseArray = [responseDict valueForKey:@"results"];

NSDictionary *dict;
for(int i=0; i< [responseArray count]; i++){
    dict = [responseArray objectAtIndex:i];
    NSLog(@"- %@",[responseArray objectAtIndex:i]);

}

it's always strongly dependant on your JSON generator. So I cannot tell you for sure it's always the best way of digging, but in this case it seems so. Just remember:

  • [data1,data2,data3,data4] -> array
  • {key1:data1,key2:data2} -> dictionary
chriscatfr
  • 2,592
  • 3
  • 24
  • 32
  • This is definitely getting me there. How do I keep chaining to go further down? Is this the best way to dig through for the desired array of values? – Brandon Linton May 27 '11 at 14:43
  • Thanks for the update - although that produces this exception `-[__NSArrayM allKeys]: unrecognized selector sent to instance 0x4c458d0` (note this is assuming I replace the responseDict initialization with your second line in my code above) – Brandon Linton May 27 '11 at 14:46
  • 1
    Thanks for the update! That was exactly what I was looking for...once I understood the JSON array notation and played with the updated code I was able to work with it easily. – Brandon Linton May 27 '11 at 17:38
1

From the looks of it, d is the key to another dictionary that contains they key results which contains an array with your data. Try using

NSLog(@"%@", responseDict);

to see the whole tree hierarchy.

kubi
  • 48,104
  • 19
  • 94
  • 118
  • Thanks, that's definitely helpful for easily printing the dictionary. I'd like to get to an individual property a couple levels in though and not sure the best way to do that (e.g., how to get the value of "Property1") – Brandon Linton May 27 '11 at 14:44
0

You can use the json-framework to convert the json data to an NSDictionary.

sergio
  • 68,819
  • 11
  • 102
  • 123
0

use this url http://mobileorchard.com/tutorial-json-over-http-on-the-iphone/

kubi
  • 48,104
  • 19
  • 94
  • 118
Deepesh
  • 633
  • 4
  • 11