【发布时间】:2020-09-05 20:42:22
【问题描述】:
我正在尝试访问下面的 json api 中的数据
"Products": [
{
"ProductName": "GR",
"ShortDescription": "General service epoxy mortar system that utilizes recycled glass and rapidly renewable soy based components.",
"PDSOverride": [
{
"FileName": "EcoLab Netherlands",
"FileUrl": "http://test.stonhard.com/media/2264/eco-lab-netherlands-usa-version.pdf"
},
{
"FileName": "General Dynamics.pdf",
"FileUrl": "http://test.stonhard.com/media/2060/general-dynamics.pdf"
}
]
}
]
我在下面这样的结构中对此进行建模
struct Solutions: Codable, Identifiable {
let id = UUID()
let SectionTitle: String
let SectionImage: String
let ProductLines: [ProductLine]
}
struct ProductLine: Codable {
let System: String
let Products: [Product]
}
struct Product: Codable {
let ProductName: String
let ShortDescription: String
let PDSOverride: [PDSOverride]
}
struct PDSOverride: Codable {
let FileName: String
let FileUrl: String
}
struct SdsPdf: Codable {
let FileName: String
let FileUrl: String
}
struct GuideSpecPdf: Codable {
let FileName: String
let FileUrl: String
}
当我尝试访问数据时,我收到一条错误消息,指出无法读取数据,因为它的格式不正确。我知道这是我的模型的问题,因为当我注释掉 PDSOverride、SdsPdf 和 GuideSpecPdf 时它可以工作,但显然我无权访问这些数据。如何对我的结构进行建模以便提取数据?
【问题讨论】:
-
您发布的 JSON 不是有效的 JSON。那是您收到的完整 JSON 吗?如果没有,您能否发布完整 JSON 的一部分以使 JSON 仍然有效?
-
转到 app.quicktype.io 并插入您的 JSON。它会为它构建一组结构,你可以将它们与你的比较,看看你的问题在哪里(或者你可以使用它生成的代码)
标签: ios json swift xcode swift5