IOS 5 JSON解析子级别

时间:2012-01-26 19:10:29

标签: iphone json ios5

我尝试使用IOS 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"];

但我如何才能访问附件&gt;图像&gt;完整&gt;网址?

我迷失了,JSON的新人......

感谢您的帮助

2 个答案:

答案 0 :(得分:8)

没有测试,但我相信它会是。

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该对象将是{{1}}。

答案 1 :(得分:0)

你也可以通过

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

记住Objective-c实际上是具有面向对象概念的c语言。您也可以应用基本的c代码或基本的c语法。 :)