-2

I am trying to decode a nested JSON. All values are mapped to 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

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

"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()
Taimur Ajmal
  • 2,778
  • 6
  • 39
  • 57

2 Answers2

2

You've skipped a level. Your Property type needs to have a property property whose value is, let's say, a PropertyDetail. In PropertyDetail, that's where id and updateTimestamp and so on need to go.

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
    }
}
matt
  • 515,959
  • 87
  • 875
  • 1,141
  • 1
    I don't see your point. You changed the question, and you didn't do what I said. – matt Jun 17 '19 at 19:37
  • You need to leave your question the way it was. Otherwise my answer doesn't relate to it. – matt Jun 17 '19 at 19:59
  • I couldn't add the code in comments here - so I changed it as you suggested. – Taimur Ajmal Jun 17 '19 at 20:01
  • Yes but that doesn't mean overwriting your original question. You can _append_ your new attempt to your original question. But to alter the heart of your question makes a mockery of the question-answer process. If you do that I'll just delete my answer and walk away (and I'll probably call for a moderator). – matt Jun 17 '19 at 20:05
  • I apologise - I have kept the original structure same and added the modified in the revised structure. Can you please have a look - It's still not working – Taimur Ajmal Jun 17 '19 at 20:20
  • 1
    Because you didn't listen to what I said. Okay, I'll add some code. But really, you should try listening. It's not our job to write your code for you. – matt Jun 17 '19 at 20:37
0

Your decode code is right, you probably have a name different of your json result.

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)
Wesley Brito
  • 143
  • 9
  • 2
    Don't encourage the use of a force try. OP handles the throwable function perfectly, nesting the `try` in a `do-catch` block and handling the error. The issue is that the incorrect types are masked by setting up all properties as `Optional`, hence no error being thrown. – Dávid Pásztor Jun 17 '19 at 19:26
  • Yeah I have a catch block – Taimur Ajmal Jun 17 '19 at 19:31
  • As a rule, avoid bang operators in Swift. You've already got a `try`, so put a `catch` in. – Adrian Jun 17 '19 at 19:38
  • I'm not saying that force that try solve the issue, i suggest put ! to force for him can see which property is wrong, because in big json with many levels you probably will forget a level. The suggestion is for he can deal with the problem alone, not for someone posting the solution to it ... – Wesley Brito Jun 19 '19 at 13:55