iphone - iOS 5 JSON 分级别解析

标签 iphone json ios5

我尝试使用 IOS 5 解析 Json feed。

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

但是我如何才能访问附件>图像>完整> url字段?

我迷路了,刚接触 JSON...

感谢您的帮助

最佳答案

还没有测试过,但我相信会的。

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。

关于iphone - iOS 5 JSON 分级别解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9023815/

相关文章:

ios - 对 MKPointAnnotation 执行后端操作

iphone - 在 iPhone 中使用 NSURLRequest 下载文件的剩余时间

html - jquery ajax 在 IE9 中导致内存泄漏

iphone - ABAddressBookCreate() , ABAddressBookGetGroupCount , ... 返回 @"0x00000000 <nil>"?

php - 字段 "to"必须是 JSON 字符串 [Firebase]

python - 将 python 字典附加到 csv

ios5 - UIButtonTypeInfoLight 在哪里?

ios - 无法在 iOS 13 上运行我的应用程序。对于框架问题

ios - 如何在 swift 中使用 AnyObject 将结构转换为对象

iphone - 给定字体的字符串长度以适应 UITextView