Parsing json in an iOS app

I have a Rest Response like this

{
  "data": [
  {
  "id": "adhoc_Expense_process:1:43",
  "key": "adhoc_Expense_process",
  "name": "Expense process",
  "version": 1,
  "deploymentId": "10",
  "resourceName": "org\/activiti\/examples\/adhoc\/Expense_process.bpmn20.xml",
  "diagramResourceName": "org\/activiti\/examples\/adhoc\/Expense_process.png",
  "startFormResourceKey": null,
  "graphicNotationDefined": "true"
},
{.
.
.
},
]
}

I am handling the response like this.

- (void)request:(RKRequest*)request didLoadResponse:(RKResponse*)response {  

    if ([request isGET]) {  
        // Handling GET /foo.xml  

        if ([response isOK]) {  
            // Success! Let's take a look at the data  

            NSLog(@"Retrieved XML: %@", [response bodyAsString]);  
            NSString *str = [response bodyAsString];

            NSDictionary *json = [str JSONValue];

            // Get all object
            NSArray *items = [json valueForKeyPath:@"data"];

            NSEnumerator *enumerator = [items objectEnumerator];
            NSDictionary* item;
            while (item = (NSDictionary*)[enumerator nextObject]) {
                NSLog(@"clientId = %@",  [item objectForKey:@"id"]);
                NSLog(@"clientName = %@",[item objectForKey:@"key"]);
                NSLog(@"job = %@",       [item objectForKey:@"version"]);
            }

        }  

    }

When i log the response with bodyAsString i can see the proper xml response on the console.But i am unable to parse it.I am using SBJSON.h or in other words the SBJSON framework.

Do you see anything wrong in the way i am parsing it.The error messages are like this..

2011-11-29 17:09:53.601 Views[6166:fb03] -[__NSCFString JSONValue]: unrecognized selector sent to instance 0x71c9200
2011-11-29 17:09:53.602 Views[6166:fb03] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString JSONValue]: unrecognized selector sent to instance 0x71c9200'

Answers


Are you importing SBJSON.h in this file? In order for the compiler to see the category that implements those methods, you have to import SBJSON.h in every source file of yours that wants to use those methods.


I added the following in BuildPhases settings of the project.

sbjson-ios in "target dependencies" libsbjson-ios.a in "link binary with libraries".

I realised i added some such ones when i added the restkit framework. Thanks guys,it helped.

Anusha


Need Your Help

How to escape a RegEx that generates an error of “unexpected quantifier"on IE?

asp.net regex internet-explorer

I use asp.net and C#. I have TextBox with an Validation Control with RegEx.

Authenticated user and multiple requests (IIS7 MVC3)

asp.net-mvc silverlight iis-7

This is one of those questions that maybe should go so serverfault, but then maybe there is a code-level solution.