我正在尝试解码JSON响应,但出现错误消息:
由于数据格式不正确,因此无法读取
响应位于["Product"]
节点中的数组中。我收到响应,但是我认为["PictureCollection"]
中的节点由于格式不正确而无法正确解码。
这是API响应。某些API对象尚不需要。仅包含在Product.swift模型中的属性。
"RMessage" : "OK",
"RSuccess" : true,
"RValue" : null,
"InputKeyword" : “Soap”,
"ProductSearchMode" : 4,
"Product" : [
{
"MinPrice" : 2000,
"Gname" : “Soap Brand 1”,
"MaxPrice" : 3190,
"IconFlgList" : [
],
"SoldoutFlg" : null,
"PictureCollection" : {
"Count" : 1,
"URL" : [
"https:someURL.jpg"
]
},
"ProgramBroadcastDate" : null,
"ID" : 107,
"Icon2OffValue" : “555”,
"Gcode" : “3333”
},
{
"Gcode" : “3334”,
"IconFlgList" : [
],
"PictureCollection" : {
"Count" : 1,
"URL" : [
"https:https:someURL1.jpg"
]
},
"MaxPrice" : 2100,
"SoldoutFlg" : null,
"Icon2OffValue" : “551”,
"ProgramBroadcastDate" : null,
"ID" : 108,
"MinPrice" : 2001,
"Gname" : "Soap Brand 2”
这是我的代码:
struct Product: Codable {
var id : Int!
var gcode : String!
var gname : String!
var minPrice : Int!
var maxPrice : Int!
var pictureCollection : PictureCollection
enum CodingKeys : String, CodingKey {
case id = "ID"
case gcode = "GCode"
case gname = "Gname"
case minPrice = "MinPrice"
case maxPrice = "MaxPrice"
case pictureCollection = "PictureCollection"
}
struct PictureCollection : Codable {
var Count : Int!
var URL : String!
}
var product : Product!
var productArray = [Product]()
let jsonResult = JSON(data)
for json in jsonResult["Product"].arrayValue {
let jsonData = try json.rawData()
self.product = try JSONDecoder().decode(Product.self, from: jsonData)
self.productArray.append(self.product)
}
答案 0 :(得分:-1)
问题出在struct PictureCollection
上。
struct PictureCollection : Codable {
var count : Int!
var url : String!
}
在您提供的 JSON 中,URL
是array
中的String
。但是在模型PictureCollection
中,您正在使用URL
类型的String
。
因此,您的struct PictureCollection
应该是
struct PictureCollection : Codable {
var count : Int
var url : [String]
}
而且,这里不需要使用强制展开类型(!
)。避免不必要地使用它们。可能会导致运行时异常。
此外,将对decode(_:from:)
的调用嵌入到do-catch
块中,并在error
块中打印整个catch
对象,以查找问题的确切原因,即
do{
//decode the JSON here...
} catch {
print(error)
}
答案 1 :(得分:-3)
我在下面添加代码,请使用此代码:
class Welcome: Codable {
let rMessage: String
let rSuccess: Bool
let rValue:Int?
let inputKeyword: String
let productSearchMode: Int
let product: [Product]
enum CodingKeys: String, CodingKey {
case rMessage = "RMessage"
case rSuccess = "RSuccess"
case rValue = "RValue"
case inputKeyword = "InputKeyword"
case productSearchMode = "ProductSearchMode"
case product = "Product"
}
init(rMessage: String, rSuccess: Bool, rValue: JSONNull?, inputKeyword: String, productSearchMode: Int, product: [Product]) {
self.rMessage = rMessage
self.rSuccess = rSuccess
self.rValue = rValue
self.inputKeyword = inputKeyword
self.productSearchMode = productSearchMode
self.product = product
}
}
//标记:-产品
class Product: Codable {
let minPrice: Int
let gname: String
let maxPrice: Int
let iconFlgList: [String]
let soldoutFlg:bool?
let pictureCollection: PictureCollection
let programBroadcastDate:String?
let id: Int
let icon2OffValue, gcode: String
enum CodingKeys: String, CodingKey {
case minPrice = "MinPrice"
case gname = "Gname"
case maxPrice = "MaxPrice"
case iconFlgList = "IconFlgList"
case soldoutFlg = "SoldoutFlg"
case pictureCollection = "PictureCollection"
case programBroadcastDate = "ProgramBroadcastDate"
case id = "ID"
case icon2OffValue = "Icon2OffValue"
case gcode = "Gcode"
}
init(minPrice: Int, gname: String, maxPrice: Int, iconFlgList: [JSONAny], soldoutFlg: JSONNull?, pictureCollection: PictureCollection, programBroadcastDate: JSONNull?, id: Int, icon2OffValue: String, gcode: String) {
self.minPrice = minPrice
self.gname = gname
self.maxPrice = maxPrice
self.iconFlgList = iconFlgList
self.soldoutFlg = soldoutFlg
self.pictureCollection = pictureCollection
self.programBroadcastDate = programBroadcastDate
self.id = id
self.icon2OffValue = icon2OffValue
self.gcode = gcode
}
}
// MARK:-PictureCollection
class PictureCollection: Codable {
let count: Int
let url: [String]
enum CodingKeys: String, CodingKey {
case count = "Count"
case url = "URL"
}
init(count: Int, url: [String]) {
self.count = count
self.url = url
}
}