2012-01-26 2 views
3

I Try pour analyser un flux JSON avec IOS 5.IOS 5 JSON avec le niveau Parsing sous

Mon JSON est comme ça:

{ 
    "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" 
    }, 

J'ai accès au titre, le contenu ... comme cela:

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

Mais comment puis-je avoir accès au champ attachement> image> full> url?

Je suis perdu, et nouvelle avec JSON ...

Merci pour votre aide

Répondre

8

n'ont pas testest mais je crois que ce serait.

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"]; 

Avec NSJSONSerialization si vous voyez [] l'objet sera un NSArray, si vous voyez {} l'objet sera un NSDictionary.

0

Vous pouvez également l'obtenir par

NSArray *posts=json[@"posts"]; 
NSDictionary *newArticle=posts[0]; 
NSString *title=newArticle[@"title"]; 

Rappelez-vous Objective-c est en fait le langage c avec des concepts orientés objet. Vous pouvez également appliquer un code c basique ou une syntaxe c basique. :)