gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 12:05:54 24 4
gpt4 key购买 nike

我是 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com