-3

I am from php domain, and now learning ios coding. I want to make a view with one part showing user info, and a table showing friends details.

I can get the json Logged correctly.

My json looks like this:

{"Me":
      {"username":"aVC",
       "userID":1
      },
 "Friends":
      [{"username":"Amm",
       "userID":2
       },...

       ]
}

Here is what I use.

 NSError *error;
    NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
    NSDictionary *json = (NSDictionary*)[NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];
    NSLog(@"jS: %@", json);

this works fine. I want to seperate the two sections (Me, and friends), and then use it to fill tables. Can someone throw some ideas?

NOTE: I am not using any frameworks. Just NSJSONSerialization.

Gabriel.Massana
  • 8,165
  • 6
  • 62
  • 81
aVC
  • 2,254
  • 2
  • 24
  • 46
  • Use NSLog to examine the dictionary (which will look a lot like the original JSON). Extract the individual dictionary elements as needed, and further process them. It's really quite simple if you just take it one step at a time. – Hot Licks May 01 '13 at 02:57

1 Answers1

1

Try adding this code after obtaining the json dictionary:

NSDictionary *me = [json objectForKey:@"Me"];
NSArray *friends = [json objectForKey:@"Friends"];

This should let you pull the information from the @"Me" and @"Friends" into separate variables.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • `friends` isn't a dictionary. – Hot Licks May 01 '13 at 02:58
  • @HotLicks You're right, I missed these square brackets, it looks like an array of dictionaries. – Sergey Kalinichenko May 01 '13 at 02:59
  • @dasblinkenlight Thanks. The "Friends" part can be one or more, so the square bracket may not be there all the time. Does that matter? Also, how would I access the individual elements? say first friend, id, etc? – aVC May 01 '13 at 03:05
  • @aVC I would assume that square brackets would always be there, even when the number of friends is one or zero: it makes sense to pass it as an array. Once you have an array of friends, you can use all methods of `NSArray` to access its individual elements: `NSDictionary *firstFriend = [friends objectAtIndex:0]`, `NSNumber *myId = [me objectForKey:@"userID"]`, and so on. – Sergey Kalinichenko May 01 '13 at 03:09
  • @dasblinkenlight Thanks very much. I think I got the idea now. :) – aVC May 01 '13 at 03:14