【发布时间】:2019-01-05 11:48:06
【问题描述】:
struct APOD: Codable {
let points: String
let full_name: String
let description: String
}
let decoder = JSONDecoder()
let product = try! decoder.decode(APOD.self, from: jsonData.data(using: .utf8)!)
print(product.full_name)
我有一个名为 jsonData 的字符串,来自:https://www.instagram.com/georgeanisimow/?__a=1。我格式化了文件并将其粘贴到项目中,只是为了有一些工作。
不幸的是它失败了这个错误代码:
“线程 1:致命错误:'try!'表情意外地升起了 错误:Swift.DecodingError.keyNotFound(编码键(字符串值: “点”,intValue:无),Swift.DecodingError.Context(codingPath:[], debugDescription:“没有与键关联的值 CodingKeys(stringValue: \"points\", intValue: nil) (\"points\").", 底层错误:nil))"
我正在尝试在 JSON 中打印“full_name”的值。
这是 JSON 的开始:
let jsonData ="""
{
"logging_page_id":"profilePage_592027119",
"show_suggested_profiles":false,
"graphql":{
"user":{
"biography":"- Represented by AEFH Talent and CESD Modeling - I travel a lot -",
"blocked_by_viewer":false,
"country_block":false,
"external_url":null,
"external_url_linkshimmed":null,
"edge_followed_by":{
"count":4571
},
"followed_by_viewer":true,
"edge_follow":{
"count":741
},
"follows_viewer":true,
"full_name":"George Anisimow"
}
}
}"""
【问题讨论】:
-
错误很明显:JSON 的根对象中没有键
points。该链接不显示任何内容。 -
@vadian 抱歉,没有意识到,不管怎样,我已经将 JSON 粘贴到项目中,没有获取数据。
-
请编辑问题并添加(开头的)JSON。
-
@vadian 刚刚这样做了
-
在您在问题中添加的
JSON中包含points, full_name, description键。
标签: ios json swift codable jsondecoder