0

I use JSONKit to parse data from a wordpress blog.

NSData *wp_json = [NSURLConnection sendSynchronousRequest: request returningResponse:&response error:&err];
NSDictionary *posts = [wp_json objectFromJSONData]

the data received is JSON so when I do this it works perfectly

NSLog(@"%@",[posts objectForKey:@"count"]);

then I want to access the posts and there is the problem posts is a sub json (if I can say that) and there is X posts so I can get a big string with all the code and I don't know how to get only the id for the first post then the id for the 2nd post.

How can I do that?

The JSON response looks like this so it will be easier to understand.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Michaël
  • 11
  • 2

1 Answers1

1

Your raw JSON you get back is simply a dictionary, and the posts are an array held in that dictionary.

NSDictionary * JSONResponse = [wp_json objectFromJSONData];
NSArray * posts = [JSONResponse objectForKey:@"posts"];

Each element in that array is yet another dictionary, but this time representing a post. You can iterate through it to do what you want with each post like so:

for(NSDictionary * post in posts) {
    //do what you want to do for each post, e.g.
    NSNumber *postId = [post objectForKey:@"id"];
}
Evan Davis
  • 462
  • 1
  • 4
  • 9