[英]Decoding nested JSON in Swift
I am trying to decode a nested JSON.我正在尝试解码嵌套的 JSON。 All values are mapped to nil.
所有值都映射到 nil。 Can someone please help me with this.
有人可以帮我解决这个问题。
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 {
}
Revised to as I missed 1 level修改为因为我错过了 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 Response 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
}
}
This is how I'm calling这就是我打电话的方式
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()
You've skipped a level.你跳过了一个级别。 Your Property type needs to have a
property
property whose value is, let's say, a PropertyDetail.您的 Property 类型需要有一个
property
属性,其值是一个 PropertyDetail。 In PropertyDetail, that's where id
and updateTimestamp
and so on need to go.在 PropertyDetail 中,这就是
id
和updateTimestamp
等需要去的地方。
So for example:例如:
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
}
}
Your decode code is right, you probably have a name different of your json result.您的解码代码是正确的,您的名称可能与 json 结果不同。
put a !放一个! after your "try" to force your code a crash, it will shows wich property is wrong.
在您“尝试”强制您的代码崩溃后,它会显示该属性是错误的。
let response = try! decoder.decode(CarouselListings.Property.self, from: data)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.