2012-01-26 42 views
3

我試着分析JSON飼料與IOS 5IOS 5 JSON解析與子水位

我的JSON文件是這樣的:

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

我有機會獲得標題,內容...像即:

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

但我如何能夠獲得附件> image> full> url字段?

我迷路了,和新的使用JSON ...

感謝您的幫助

回答

8

有沒有testest但我相信這將是。

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

隨着NSJSONSerialization如果你看到[]對象將是一個NSArray,如果你看到{}對象將是一個NSDictionary

0

您還可以通過

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

得到它記住的Objective-C實際上是用面向對象的概念,C語言。您也可以應用基本的C代碼或基本的C語法。 :)