gpt4 book ai didi

ios - 我的 JSON 响应的某些属性未加载到我的模型中

转载 作者:行者123 更新时间:2023-11-28 19:25:28 25 4
gpt4 key购买 nike

访问我的 JSON 响应的几个属性时出现以下错误

valueNotFound(Swift.String, Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "articles", intValue: nil), _JSONKey(stringValue: "Index 15", intValue: 15), CodingKeys(stringValue: "urlToImage", intValue: nil)], debugDescription: "Expected String value but found null instead.", underlyingError: nil))

在以下 JSON 响应中,作者、urlToimage 和内容等出现上述错误。我无法找出原因。

"author": "PTI",
"title": "Rape Survivor, Set Ablaze in UP's Unnao a Day Ago, Critical; Doctors Say Her Vitals Are 'Very Low' - News18",
"description": "The Delhi Traffic Police on Thursday provided a 'green corridor' for hindrance-free movement of the ambulance carrying her from the airport to the hospital.",
"url": "https://www.news18.com/news/india/rape-survivor-set-ablaze-in-ups-unnao-a-day-ago-on-ventilator-at-delhi-hosp-docs-say-shes-critical-2414083.html",
"urlToImage": "https://images.news18.com/ibnlive/uploads/2019/12/Safdarjung-hospital-Unnao-rape.jpg",
"publishedAt": "2019-12-06T05:58:00Z",
"content": "New Delhi: The rape survivor from Unnao, who was airlifted to Delhi and admitted to the Safdarjung Hospital here, is extremely critical and on ventilator, doctors attending to her said on Friday.
"The condition of the patient is extremely critical and she is… [+1187 chars]"

我正在使用以下结构模型。

struct Article: Decodable {

let author: String
let title: String
let description: String
let urlToImage: String
let publishedAt: String
let content: String
}

请帮我解决这个问题。

最佳答案

请仔细阅读错误信息。

明确指出在数组的第16项(JSONKey(stringValue: "Index 15", intValue: 15)) articles (CodingKeys(stringValue: "articles") 没有值 (valueNotFound/预期的字符串值,但发现为 null) for key urlToImage (CodingKeys(stringValue: "urlToImage ")

在结构中声明 urlToImage 为可选

let urlToImage: String?

旁注:

您可以将 urlurlToImage 声明为 URL 而无需任何更改,并将 publishedAt 声明为 Date 通过添加 iso8601 日期解码策略。

关于ios - 我的 JSON 响应的某些属性未加载到我的模型中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59211650/

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