使用 codable 和 CodingKeys swift 解析 JSON
Parse JSON using codable and CodingKeys swift
我尝试了很多教程,但无法解决。
我有 JSON 这样的结构。
{
"Region":[
{
"head":[
{
"total_count":572888
},
{
"RESULT":{
"CODE":"000",
"MESSAGE":"Success."
}
},
{
"api_version":"1.0"
}
]
},
{
"row":[
{
"COM_NAME":"company",
"TYPE_CD":null,
"BIZ_NM":null,
"TYPE_NM":null,
"TELNO":"111-1111-1111",
"MNY_NM":null,
"POSBL_YN":null,
"DATE":"2018-09-30",
"ROAD_ADDR":"adrress",
"ZIP_CD":"10303",
"LOGT":"126.80090346",
"LAT":"37.673069059",
}
]
}
]
}
我想做的是从行部分解码一些属性。
所以我创建了一些这样的结构。
struct List: Decodable {
let Region: [Stores]
}
struct Stores: Decodable {
let row: [StoreInfo]
}
struct StoreInfo: Decodable {
let COM_NAME: String?
let TYPE_NM: String?
let TELNO: String?
let DATE: String?
let ROAD_ADDR: String?
let LOGT: String?
let LAT: String?
}
然后像这样用 Jsondecoder 解码。
let decodedData = try JSONDecoder().decode(List.self, from: data)
print(decodedData)
我收到这样的错误。
keyNotFound(CodingKeys(stringValue: "Region", intValue: nil),
Swift.DecodingError.Context(codingPath: [], debugDescription: "No value associated with key CodingKeys(stringValue: \"Region\", intValue: nil)
(\"Region\").", underlyingError: nil))
如何正确解码?
数组 "Region" 包含不同的类型,在 swift 中它将被声明为 [Any],因此您需要将行声明为可选的,以便在解码器找到并尝试解码元素时"head" 它只会忽略该元素并解码下一个元素。
struct Stores: Decodable {
let row: [StoreInfo]?
}
PS
您发布的错误不是由发布的代码生成的,发布的代码的真正错误是
keyNotFound(CodingKeys(stringValue: "row", intValue: nil), Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "Region", intValue: nil), _JSONKey(stringValue: "Index 0", intValue: 0)], debugDescription: "No value associated with key CodingKeys(stringValue: \"row\", intValue: nil) (\"row\").", underlyingError: nil))
DS
我将商店和商店信息用作可选,并使用 Codable 创建了结构并且它起作用了。
struct List: Codable {
let Region: [Stores]?
}
struct Stores: Codable {
let row: [StoreInfo]?
}
struct StoreInfo: Codable {
let COM_NAME: String?
let TYPE_NM: String?
let TELNO: String?
let DATE: String?
let ROAD_ADDR: String?
let LOGT: String?
let LAT: String?
}
我尝试了很多教程,但无法解决。
我有 JSON 这样的结构。
{
"Region":[
{
"head":[
{
"total_count":572888
},
{
"RESULT":{
"CODE":"000",
"MESSAGE":"Success."
}
},
{
"api_version":"1.0"
}
]
},
{
"row":[
{
"COM_NAME":"company",
"TYPE_CD":null,
"BIZ_NM":null,
"TYPE_NM":null,
"TELNO":"111-1111-1111",
"MNY_NM":null,
"POSBL_YN":null,
"DATE":"2018-09-30",
"ROAD_ADDR":"adrress",
"ZIP_CD":"10303",
"LOGT":"126.80090346",
"LAT":"37.673069059",
}
]
}
]
}
我想做的是从行部分解码一些属性。
所以我创建了一些这样的结构。
struct List: Decodable {
let Region: [Stores]
}
struct Stores: Decodable {
let row: [StoreInfo]
}
struct StoreInfo: Decodable {
let COM_NAME: String?
let TYPE_NM: String?
let TELNO: String?
let DATE: String?
let ROAD_ADDR: String?
let LOGT: String?
let LAT: String?
}
然后像这样用 Jsondecoder 解码。
let decodedData = try JSONDecoder().decode(List.self, from: data)
print(decodedData)
我收到这样的错误。
keyNotFound(CodingKeys(stringValue: "Region", intValue: nil),
Swift.DecodingError.Context(codingPath: [], debugDescription: "No value associated with key CodingKeys(stringValue: \"Region\", intValue: nil)
(\"Region\").", underlyingError: nil))
如何正确解码?
数组 "Region" 包含不同的类型,在 swift 中它将被声明为 [Any],因此您需要将行声明为可选的,以便在解码器找到并尝试解码元素时"head" 它只会忽略该元素并解码下一个元素。
struct Stores: Decodable {
let row: [StoreInfo]?
}
PS
您发布的错误不是由发布的代码生成的,发布的代码的真正错误是
keyNotFound(CodingKeys(stringValue: "row", intValue: nil), Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "Region", intValue: nil), _JSONKey(stringValue: "Index 0", intValue: 0)], debugDescription: "No value associated with key CodingKeys(stringValue: \"row\", intValue: nil) (\"row\").", underlyingError: nil))
DS
我将商店和商店信息用作可选,并使用 Codable 创建了结构并且它起作用了。
struct List: Codable {
let Region: [Stores]?
}
struct Stores: Codable {
let row: [StoreInfo]?
}
struct StoreInfo: Codable {
let COM_NAME: String?
let TYPE_NM: String?
let TELNO: String?
let DATE: String?
let ROAD_ADDR: String?
let LOGT: String?
let LAT: String?
}