[英](Swift) Unable to decode JSON starting with { "something" : [ {
Below is a sample of the JSON I am trying to decode:下面是我试图解码的 JSON 的示例:
{
"results": [
{
"Modified": "2022-12-30T22:27:00",
"Published": "2022-12-23T15:15:00",
"access": {},
"assigner": "cve@mitre.org",
"cvss": null,
"cwe": "CWE-77",
"id": "CVE-2022-46642",
"impact": {},
"last-modified": "2022-12-30T22:27:00",
"references": [
"https://github.com/CyberUnicornIoT/IoTvuln/blob/main/d-link/dir-846/D-Link%20dir-846%20SetAutoUpgradeInfo%20command%20injection%20vulnerability.md",
"https://www.dlink.com/en/security-bulletin/"
],
"summary": "D-Link DIR-846 A1_FW100A43 was discovered to contain a command injection vulnerability via the auto_upgrade_hour parameter in the SetAutoUpgradeInfo function.",
"vulnerable_configuration": [
"cpe:2.3:o:dlink:dir-846_firmware:100a43:*:*:*:*:*:*:*",
"cpe:2.3:h:dlink:dir-846:a1:*:*:*:*:*:*:*"
],
"vulnerable_configuration_cpe_2_2": [],
"vulnerable_product": [
"cpe:2.3:o:dlink:dir-846_firmware:100a43:*:*:*:*:*:*:*"
]
},
(the JSON file continues with the next { "Modified"...
) (JSON 文件继续下一个{ "Modified"...
)
I searched for a solution and my code is as follows:我搜索了一个解决方案,我的代码如下:
The struct结构
struct CVE : Decodable
{
var id : String
var cvss : String? = nil
var Modified : String
var summary : String
}
struct CVEdata : Decodable
{
var results : [CVE]
}
and the JSON decoding function和 JSON 解码 function
func jsonDataRequest () async
{
if let url = URL(string: "https://cve.circl.lu/api/query?time_start=30-12-2022,time_end=31-12-2022")
{
do
{
let (data, _) = try await URLSession.shared.data(from: url)
arrCVE = try JSONDecoder().decode([CVEdata].self, from: data)
}
catch
{
print(error)
}
}
}
}
If the above algorithm is correct, how can I access the data in the arrCVE variable?如果上面的算法是正确的,我该如何访问arrCVE变量中的数据呢? I have tried a for loop for dictionary, but I think the issue is somewhere in the decoding line.我已经为字典尝试了一个 for 循环,但我认为问题出在解码行的某个地方。 Thank you谢谢
The problem here is that the top-level container of your JSON is not an array, so for start your decoding type should be CVEData.self
(instead of [CVEData].self
).这里的问题是你的 JSON 的顶级容器不是一个数组,所以首先你的解码类型应该是CVEData.self
(而不是[CVEData].self
)。
Then you can access the list of CVE objects via arrCVE.results
.然后您可以通过arrCVE.results
访问 CVE 对象列表。
Don't forget that arrCVE
must be redeclared accordingly:不要忘记必须相应地重新声明arrCVE
:
var arrCVE: CVEdata?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.