Patrick
Patrick

Reputation: 7185

Getting contents of complex NSDictionary

I am calling an API which returns some high level data in JSON format. This is then being processed into an NSDictionary like so;

NSDictionary *results = [NSJSONSerialization JSONObjectWithData:JSONData options:kNilOptions error:&error];

When I log this I can a very complicated result similar to this:

{
 one = {
         oneone = 12345;
         onetwo = "Hello";
         onethree = "How Are You";
       };
 two = 42;
 three = {
           threeone = {
                        threeoneone = "Name";
                        threeonetwo = {
                                          threeonetwoone = "100";
                                          threeonetwotwo = "26";
                                       };
etc etc etc

Now I get the contents of 'three' by calling [results objectForKey:@"three"] but how do I get the value of 'threeonetwoone'?

I have tried a few things but to no avail.

Upvotes: 1

Views: 152

Answers (3)

Anoop Vaidya
Anoop Vaidya

Reputation: 46533

Use something like this... please check it in compiler

As I am not sure, you have mentioned correct dictionary or not, and all your keys should be correct.

[results valueForKeyPath:@"three.threeone.threeonetwo.threeonetwoone"];

Upvotes: 5

Ramy Al Zuhouri
Ramy Al Zuhouri

Reputation: 21966

In your case:

NSString* threeoneone= dict[@"three"][@"threeone"][@"threeonetoone"]; 

Upvotes: 1

Nikola Kirev
Nikola Kirev

Reputation: 3152

With the recent changes in Objective-C syntax you can do it like that:

results[@"three"][@"threeone"][@"threeonetwo"][@"threeonetwoone"];

Upvotes: 2

Related Questions