解码 Swift 中嵌套的 JSON
Decoding nested JSON in Swift
我正在尝试解码嵌套 JSON。所有值都映射到 nil。
有人可以帮我解决这个问题吗?
struct CarouselListings: Decodable {
var success: Bool
var elapsed: Int = 0
let result: Result
struct Result: Decodable {
let properties: [Property]?
let neighborhoods: [Neighborhood]?
let buildings: [Building]?
let communities: [Community]?
}
struct Property: Decodable {
var id:Int?
var updateTimestamp : String?
var name : String?
var description : String?
struct PropertyType: Codable {
var id:Int?
var name = ""
var description = ""
}
var latitude: String?
var longitude: String?
var thumbnailImage: String?
var buildingId: Int?
var communityId: Int?
var neighborhoodId: Int64?
var mint: Bool?
var active: Bool?
}
struct Neighborhood: Codable {
}
struct Building: Codable {
}
struct Community: Codable {
}
修改为因为我错过了 1 级
struct CarouselListings: Decodable {
var success: Bool
var elapsed: Int = 0
let result: Result
struct Result: Decodable {
let properties: [Propertu]?
let neighborhoods: [Neighborhood]?
let buildings: [Building]?
let communities: [Community]?
}
struct Properties: Decodable {
let propertyDetail: Property?
let images = [String]()
}
struct Neighborhoods: Decodable {
let neighborhoodDetails: Neighborhood]?
}
struct Buildings: Decodable {
let buildingDetail: Building?
}
struct Communities: Decodable {
let communitieDetail: Community?
}
struct Property: Decodable {
var id:Int?
var updateTimestamp : String?
var name : String?
var description : String?
struct PropertyType: Codable {
var id:Int?
var name = ""
var description = ""
}
var latitude: String?
var longitude: String?
var thumbnailImage: String?
var buildingId: Int?
var communityId: Int?
var neighborhoodId: Int64?
var mint: Bool?
var active: Bool?
}
struct Neighborhood: Codable {
}
struct Building: Codable {
}
struct Community: Codable {
}
}
JSON 回应
"success": true,
"elapsed": 20,
"result": {
"neighborhoods": null,
"properties": [
{
"property": {
"id": 1,
"updateTimestamp": null,
"name": "Al Deyar",
"description": "Al Deyar Villa",
"propertyType": {
"id": 709415277471,
"name": "villa",
"description": "villa"
},
"latitude": "1",
"longitude": "2",
"thumbnailImage": "https://q-ak.bstatic.com/images/hotel/max1280x900/167/167547890.jpg",
"video": null,
"buildingId": 1,
"communityId": 1,
"neighborhoodId": 1634048588303324,
"mint": true,
"active": true
},
"images": []
}
],
"buildings": null,
"communities": null
}
}
这就是我打电话的方式
URLSession.shared.dataTask(with: request) { (data, response, error) in
guard let data = data else {
return
}
do {
let decoder = JSONDecoder()
let response = try decoder.decode(CarouselListings.self, from: data)
print(response)
if let properties = response.result.properties {
successBlock(properties,response.success)
}
} catch let error {
errorBlock(error)
}
}.resume()
您的解码代码是正确的,您的名称可能与您的 json 结果不同。
放一个!在你 "try" 强制你的代码崩溃后,它会显示 属性 是错误的。
let response = try! decoder.decode(CarouselListings.Property.self, from: data)
您跳过了一个级别。您的 属性 类型需要有一个 property
属性,其值比方说是一个 属性Detail。在 属性Detail 中,这就是 id
和 updateTimestamp
等需要去的地方。
例如:
struct CarouselListings: Decodable {
var success: Bool
var elapsed: Int = 0
let result: Result
struct Result: Decodable {
let properties: [Property]?
}
struct Property: Decodable {
let property: PropertyDetail?
}
struct PropertyDetail: Decodable {
var id:Int?
var updateTimestamp : String?
var name : String?
var description : String?
// and so on
}
}
我正在尝试解码嵌套 JSON。所有值都映射到 nil。 有人可以帮我解决这个问题吗?
struct CarouselListings: Decodable {
var success: Bool
var elapsed: Int = 0
let result: Result
struct Result: Decodable {
let properties: [Property]?
let neighborhoods: [Neighborhood]?
let buildings: [Building]?
let communities: [Community]?
}
struct Property: Decodable {
var id:Int?
var updateTimestamp : String?
var name : String?
var description : String?
struct PropertyType: Codable {
var id:Int?
var name = ""
var description = ""
}
var latitude: String?
var longitude: String?
var thumbnailImage: String?
var buildingId: Int?
var communityId: Int?
var neighborhoodId: Int64?
var mint: Bool?
var active: Bool?
}
struct Neighborhood: Codable {
}
struct Building: Codable {
}
struct Community: Codable {
}
修改为因为我错过了 1 级
struct CarouselListings: Decodable {
var success: Bool
var elapsed: Int = 0
let result: Result
struct Result: Decodable {
let properties: [Propertu]?
let neighborhoods: [Neighborhood]?
let buildings: [Building]?
let communities: [Community]?
}
struct Properties: Decodable {
let propertyDetail: Property?
let images = [String]()
}
struct Neighborhoods: Decodable {
let neighborhoodDetails: Neighborhood]?
}
struct Buildings: Decodable {
let buildingDetail: Building?
}
struct Communities: Decodable {
let communitieDetail: Community?
}
struct Property: Decodable {
var id:Int?
var updateTimestamp : String?
var name : String?
var description : String?
struct PropertyType: Codable {
var id:Int?
var name = ""
var description = ""
}
var latitude: String?
var longitude: String?
var thumbnailImage: String?
var buildingId: Int?
var communityId: Int?
var neighborhoodId: Int64?
var mint: Bool?
var active: Bool?
}
struct Neighborhood: Codable {
}
struct Building: Codable {
}
struct Community: Codable {
}
}
JSON 回应
"success": true,
"elapsed": 20,
"result": {
"neighborhoods": null,
"properties": [
{
"property": {
"id": 1,
"updateTimestamp": null,
"name": "Al Deyar",
"description": "Al Deyar Villa",
"propertyType": {
"id": 709415277471,
"name": "villa",
"description": "villa"
},
"latitude": "1",
"longitude": "2",
"thumbnailImage": "https://q-ak.bstatic.com/images/hotel/max1280x900/167/167547890.jpg",
"video": null,
"buildingId": 1,
"communityId": 1,
"neighborhoodId": 1634048588303324,
"mint": true,
"active": true
},
"images": []
}
],
"buildings": null,
"communities": null
}
}
这就是我打电话的方式
URLSession.shared.dataTask(with: request) { (data, response, error) in
guard let data = data else {
return
}
do {
let decoder = JSONDecoder()
let response = try decoder.decode(CarouselListings.self, from: data)
print(response)
if let properties = response.result.properties {
successBlock(properties,response.success)
}
} catch let error {
errorBlock(error)
}
}.resume()
您的解码代码是正确的,您的名称可能与您的 json 结果不同。
放一个!在你 "try" 强制你的代码崩溃后,它会显示 属性 是错误的。
let response = try! decoder.decode(CarouselListings.Property.self, from: data)
您跳过了一个级别。您的 属性 类型需要有一个 property
属性,其值比方说是一个 属性Detail。在 属性Detail 中,这就是 id
和 updateTimestamp
等需要去的地方。
例如:
struct CarouselListings: Decodable {
var success: Bool
var elapsed: Int = 0
let result: Result
struct Result: Decodable {
let properties: [Property]?
}
struct Property: Decodable {
let property: PropertyDetail?
}
struct PropertyDetail: Decodable {
var id:Int?
var updateTimestamp : String?
var name : String?
var description : String?
// and so on
}
}