Search code examples
iphonejsonios5

IOS 5 JSON Parsing with sub level


I Try to parse a Json feed with IOS 5.

My Json File is like that :

{
  "status": "ok",
  "count": 18,
  "count_total": 2248,
  "pages": 125,
"posts": [
    {
      "id": 31781,
      "type": "post",
      "slug": "aaa",
      "url": "http:\/\/www.example.com\/videos\/aaa.html",
      "status": "publish",
      "title": "my Title",
      "title_plain": "My Title",
      "content": "<p>Jdfkdfkjkjdfklfdkldfkldfklfkdld.<\/p>\n",
      "excerpt": "Jdfkdfkjkjdfklfdkldfkldfklfkdlds.",
      "date": "2012-01-26 07:38:29",
      "modified": "2012-01-26 07:38:29",
      "categories": [
        {
          "id": 4,
          "slug": "videos",
          "title": "Videos",
          "description": "",
          "parent": 0,
          "post_count": 476
        }
      ],
      "tags": [],
      "author": {
        "id": 4,
        "slug": "author",
        "name": "Au Thor",
        "first_name": "",
        "last_name": "",
        "nickname": "Au Thor",
        "url": "",
        "description": ""
      },
      "attachments": [
        {
          "id": 31784,
          "url": "http:\/\/www.example.com\/wp-content\/uploads\/2012\/01\/primo.jpg",
          "slug": "primo",
          "title": "primo",
          "description": "",
          "caption": "",
          "parent": 31781,
          "mime_type": "image\/jpeg",
          "images": {
            "full": {
              "url": "http:\/\/www.example.com\/wp-content\/uploads\/2012\/01\/primo.jpg",
              "width": 620,
              "height": 389
            },
            "thumbnail": {
              "url": "http:\/\/www.example.com\/wp-content\/uploads\/2012\/01\/primo-150x75.jpg",
              "width": 150,
              "height": 75
            },
            "medium": {
              "url": "http:\/\/www.example.com\/wp-content\/uploads\/2012\/01\/primo.jpg",
              "width": 620,
              "height": 389
            },
            "large": {
              "url": "http:\/\/www.example.com\/wp-content\/uploads\/2012\/01\/primo.jpg",
              "width": 620,
              "height": 389
            }
          }
        }
      ],
      "comment_count": 1,
      "comment_status": "open",
      "thumbnail": "http:\/\/www.example.com\/wp-content\/uploads\/2012\/01\/primo-150x75.jpg"
    },

I have access to Title, content... like that :

NSArray* latestArticles = [json objectForKey:@"posts"];
NSDictionary* Article = [latestArticles objectAtIndex:0];
NSString *Titre = [Article objectForKey:@"title"];

But how I can have access to attachement > image > full > url field ?

I'm lost, and new with JSON...

Thanks for your help


Solution

  • Haven't testest but I believe it would be.

    NSArray *allPosts = [json objectForKey:@"posts"];
    NSDictionary *firstPost = [allPosts objectAtIndex:0];
    NSArray *allAttachments = [firstPost objectForKey:@"attachments"];
    NSDictionary *firstAttachment = [allAttachments objectAtIndex:0];
    NSDictionary *allImages = [firstAttachment objectForKey:@"images"];
    NSDictionary *fullImage = [allImages objectForKey:@"full"];
    NSString *urlString = [fullImage objectForKey:@"url"];
    

    With NSJSONSerialization if you see [] the object will be an NSArray, if you see {} the object will be an NSDictionary.