json - 在 Swift 3.0 中陷入来自 Wordpress 站点的 JSON 数据

标签 json swift

我是 Swift 新手。我无法在 Swift 应用程序中解析 JSON 数据。 我试图打印出“标题”,但它显示为零。谁能帮我解决这个问题吗?

这是我的 JSON 格式:

{  
   "status":"ok",
   "count":2,
   "pages":1,
   "category":{  },
   "posts":[  
      {  
         "id":435,
         "type":"post",
         "slug":"tips-the-thao-ngay-10102017",
         "url":"http:\/\/hongbien.online\/tips-the-thao-ngay-10102017\/",
         "status":"publish",
         "title":"Tips th\u1ec3 thao ng\u00e0y 10\/10\/2017",
         "title_plain":"Tips th\u1ec3 thao ng\u00e0y 10\/10\/2017",
         "content":"<p>Ph\u00e1p &#8211; Belarus<\/p>\n<p>\u0110\u01b0\u1ee3c thi \u0111\u1ea5u tr\u00ean s\u00e2n nh\u00e0 v\u00e0 \u0111\u00e1nh gi\u00e1 v\u01b0\u1ee3t tr\u1ed9i so v\u1edbi \u0111\u1ea5u th\u1ee7 nh\u01b0ng v\u1edbi m\u1ee9c k\u00e8o ch\u1ea5p +3 cho Belarus th\u00ec s\u1ebd r\u1ea5t kh\u00f3 kh\u0103n \u0111\u1ec3 c\u00e1c c\u1ea7u th\u1ee7 Ph\u00e1p chi\u1ec1u l\u00f2ng cho c\u00e1c nh\u00e0 \u0111\u1ea7u t\u01b0 trong b\u1ed1i c\u1ea3nh ch\u1ec9 c\u1ea7n gi\u00e0nh chi\u1ebfn th\u1eafng t\u1ed1i thi\u1ec3u l\u00e0 c\u00f3 th\u1ec3 \u0111i ti\u1ebfp.<\/p>\n<p> <a href=\"http:\/\/hongbien.online\/tips-the-thao-ngay-10102017\/#more-435\" class=\"more-link\">Read more<\/a><\/p>\n",
         "excerpt":"<p>Ph\u00e1p &#8211; Belarus \u0110\u01b0\u1ee3c thi \u0111\u1ea5u tr\u00ean s\u00e2n nh\u00e0 v\u00e0 \u0111\u00e1nh gi\u00e1 v\u01b0\u1ee3t tr\u1ed9i so v\u1edbi \u0111\u1ea5u th\u1ee7 nh\u01b0ng v\u1edbi m\u1ee9c k\u00e8o ch\u1ea5p +3 cho Belarus th\u00ec s\u1ebd r\u1ea5t kh\u00f3 kh\u0103n \u0111\u1ec3 c\u00e1c c\u1ea7u th\u1ee7 Ph\u00e1p chi\u1ec1u l\u00f2ng cho c\u00e1c nh\u00e0 \u0111\u1ea7u t\u01b0 trong b\u1ed1i c\u1ea3nh ch\u1ec9 c\u1ea7n gi\u00e0nh chi\u1ebfn th\u1eafng t\u1ed1i thi\u1ec3u l\u00e0 c\u00f3 [&hellip;]<\/p>\n",
         "date":"2017-10-11 22:38:20",
         "modified":"2017-10-11 22:38:20",
         "categories":[  ],
         "tags":[  ],
         "author":{  },
         "comments":[  ],
         "attachments":[  ],
         "comment_count":0,
         "comment_status":"open",
         "custom_fields":{  }
      },
      {  }
   ]
}

这是我的 Swift 代码:

import UIKit

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()

        let url = NSURL(string: "http://hongbien.online/category/tips-the-thao/?json=1")!
        let request = NSMutableURLRequest(url: url as URL)
        URLSession.shared.dataTask(with: request as URLRequest) { (data : Data?, urlResponse : URLResponse?, error : Error?) in
            if error != nil {
                print(error)
            }

            do {
                let json = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as! [String : AnyObject]
                let title = json["posts"]?["title"]
                print(title)
            }
            catch {
                print("Catch the error : \(error)")
            }
        }
        .resume()
    }
}

最佳答案

Change this one line. You just forgot to put index for array(json["posts"] is array)

let title = json["posts"]?[0]["title"]
print(title)

关于json - 在 Swift 3.0 中陷入来自 Wordpress 站点的 JSON 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46689549/

相关文章:

php - 查询多个 MySQL 表并将数据编码为 JSON

swift - Firebase SDK 3.x Linkedin 登录

swift - 如何为每个单元格分配一个数字?

ios - 使用起始位置和距离计算新坐标

ios - EXC_BAD_ACCESS 在 Swift 中访问自定义 MKAnnotation 的属性时

ios - CAShapeLayer 左上角缺少像素

javascript - 如何将 JSO 转换为 JSON?

json - 嵌套 json 的 Azure JMESPATH 查询

android - modelmapper 是为在 android 中使用而设计的吗?

javascript - 将数据作为 json 发布到在 Phantomjs 中不起作用的 Servlet