azamsharp
azamsharp

Reputation: 20096

Get the Value for the Key for iOS app consuming JSON

I have a webservice (.NET) which returns JSON result. The iOS application consumes that web service and gets a response. Everything works but when I get the response it is inside the object d. Here is the response:

{"d":"{\"Name\":\"azamsharp\"}"}

Now, as you can imagine I need to access the value of the property "Name" which is "azamsharp". I followed another post which suggested the following approach:

 NSLog(@"%@",[responseDict valueForKeyPath:@"d.Name"]);

But this gave me the following error:

reason: '[<NSCFString 0x4cd6270> valueForUndefinedKey:]: this class is not key value coding-compliant for the key Name.'

Any help on this issue is much appreciated!

Upvotes: 0

Views: 1881

Answers (2)

bruno
bruno

Reputation: 2169

Another aproach would be:

NSString *innerString = [[responseDict valueForKey:@"d"] valueForKey@"Name"];

You would only need a single line.

Upvotes: 2

Anomie
Anomie

Reputation: 94834

There is no property "name" in that JSON object. Your JSON object has a single key, "d", whose value is a string, which happens to be the string representation of another JSON object.

You would have to do something like this:

NSString *innerString = [responseDict valueForKey:@"d"];
NSDictionary *innerObject = [innerString parseJSON]; // Or however you parse a JSON string
NSString *name = [innerObject valueForKey:@"Name"];

Upvotes: 3

Related Questions