views:

3817

answers:

2

I'm writing an iPhone native app using the JSON framework.

My app is accessing web services using JSON. The JSON data we send has nested objects, below is an example of the data served up:

{"model":{"JSONRESPONSE":{"authenticationFlag":true,"sessionId":"3C4AA754D77BFBE33E0D66EBE306B8CA","statusMessage":"Successful Login.","locId":1,"userName":"Joe Schmoe"}}}

I'm having problem parsing using the objectForKey and valueForKey NSDictionary methods. I keep getting invalidArgumentException runtime errors.

For instance, I want to query the response data for the "authenticationFlag" element.

Thanks, Mike Seattle

+1  A: 

It is hard to tell without some more details (e.g. the JSON parsing code that you are using), but two things strike me as possible: (1) you are not querying with a full path. In the case above, you'd need to first get the enclosing model, the json response, and only then ask the json response dictionary for the authenticationFlag value:

[[[jsonDict objectForKey:@"model"] objectForKey:@"JSONRESPONSE"] objectForKey:@"authenticationFlag"]

(2) perhaps you're using c-strings ("") rather than NSStrings (@"") as keys (although this would likely crash nastily or just not compile). The key should be something than can be cast to id.

While possible, both are probably false, so please include more detail.

jm
Solution 1 worked. Essentially, I needed to traverse down to the JSONRESPONSE node then I could access the desired element. Thanks!
mibrop
A: 

Hello I'm writing an iPhone app using the JSON framework. I couldn't serialize an NSObject, I found only serialize for nsstring, nsarray, nsdata, but I want to serialize a generic object, NSObject you know any solution or any class library for this?

Thanks

ovidiu