簡體   English   中英

在 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()

你跳過了一個級別。 您的 Property 類型需要有一個property屬性,其值是一個 PropertyDetail。 在 PropertyDetail 中,這就是idupdateTimestamp等需要去的地方。

例如:

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 結果不同。

放一個! 在您“嘗試”強制您的代碼崩潰后,它會顯示該屬性是錯誤的。

let response = try! decoder.decode(CarouselListings.Property.self, from: data)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM