vote up 0 vote down
star

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

flag
add comment

1 Answer:

vote up 0 vote down
check

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.

link|flag
comments (1)

Your Answer:

Get an OpenID
or

Not the answer you're looking for? Browse other questions tagged or ask your own question.