首页 > 解决方案 > 在响应快速编码中解析普通 json 字符串值

问题描述

我想解析这个响应。解析其他所有内容 - 除了图像。我在一个字符串中收到它,但我无法将它转换成字典。这是我的模型。

struct PropertyList: Decodable {

let result: Property?
let success: Bool = false

struct Property: Decodable {
    let name: String?
    let description: String?
    let propertyType: PropertyType
    let latitude, longitude: String

    let images: String?
    let areaSize:Int?
    let threeSixtyView: String?
    let threeDModel: String?

    enum CodingKeys: String, CodingKey {
        case name
        case propertyDescription = "description"
        case propertyType, latitude, longitude
        case threeDModel = "threeDModel"
        case images = "images"
    }
}
}


struct PropertyType: Codable {
let id: Int
let name, propertyTypeDescription: String

enum CodingKeys: String, CodingKey {
    case id, name
    case propertyTypeDescription = "description"
}
}

API 响应:

        "name": "Al Deyar",
        "description": "Al Deyar Villa",
        "propertyType": {
            "id": 709415277471,
            "name": "villa",
            "description": "villa"
        },
        "latitude": "1",
        "longitude": "2",
        "viewOfWater": null,
        "threeDModel": "https://viewer.archilogic.com/?sceneId=80d4b3bb-98de-4279-a867-633bf67c6e72&s=m2fss0p3slst",
        "images": "[{\"id\": 1, \"image\":\"https://neigborhood-images.s3.amazonaws.com/property/1BEEA0B6-A2B1-4D5E-837B-9C2B00F46EE4_2048x2048.jpg\"},\n{\"id\": 2, \"image\":\"https://neigborhood-images.s3.amazonaws.com/property/984D2D29-2448-4B68-827F-EC912AB9AF14_2048x2048.jpg\"},\n{\"id\": 3, \"image\":\"https://neigborhood-images.s3.amazonaws.com/property/EBARZA-_0002_Layer_11_2048x2048.jpg\"},\n{\"id\": 4, \"image\":\"https://neigborhood-images.s3.amazonaws.com/property/EBARZA-_0002_Layer_10_ad2d92e2-3740-4d1d-8e9c-ed41cf89c3b2_2048x2048.jpg\"},\n{\"id\": 5, \"image\":\"https://neigborhood-images.s3.amazonaws.com/property/EBARZA-furniture_0001_Layer_21_2048x2048.jpg\"},\n{\"id\": 6, \"image\":\"https://neigborhood-images.s3.amazonaws.com/property/EBARZA-furniture_0044_Layer_5_2048x2048.jpg\"},\n{\"id\": 7, \"image\":\"https://neigborhood-images.s3.amazonaws.com/property/EBARZA-furniture_0042_Layer_3_2048x2048.jpg\"}]"

> Blockquote

我的模型值已更新

在此处输入图像描述

标签: iosswiftxcode

解决方案


images是一个嵌套的 JSON 字符串,必须单独解码。

一种解决方案是将包含嵌套 JSON 字符串的值声明为带有 a 的 struct ( ImageJSON)singleValueContainer并在第二级解码该字符串。

我遗漏了不在 JSON 中的属性

let json = """
{
"success" : true,
"result" : {
    "name": "Al Deyar",
    "description": "Al Deyar Villa",
    "propertyType": {
        "id": 709415277471,
        "name": "villa",
        "description": "villa"
    },
    "latitude": "1",
    "longitude": "2",
    "viewOfWater": null,
    "threeDModel": "https://viewer.archilogic.com/?sceneId=80d4b3bb-98de-4279-a867-633bf67c6e72&s=m2fss0p3slst",
    "images":"[{\\"id\\": 1, \\"image\\":\\"https://neigborhood-images.s3.amazonaws.com/property/1BEEA0B6-A2B1-4D5E-837B-9C2B00F46EE4_2048x2048.jpg\\"},{\\"id\\": 2,\\"image\\":\\"https://neigborhood-images.s3.amazonaws.com/property/984D2D29-2448-4B68-827F-EC912AB9AF14_2048x2048.jpg\\"},{\\"id\\": 3,\\"image\\":\\"https://neigborhood-images.s3.amazonaws.com/property/EBARZA-_0002_Layer_11_2048x2048.jpg\\"},{\\"id\\": 4,\\"image\\":\\"https://neigborhood-images.s3.amazonaws.com/property/EBARZA-_0002_Layer_10_ad2d92e2-3740-4d1d-8e9c-ed41cf89c3b2_2048x2048.jpg\\"},{\\"id\\": 5,\\"image\\":\\"https://neigborhood-images.s3.amazonaws.com/property/EBARZA-furniture_0001_Layer_21_2048x2048.jpg\\"},{\\"id\\": 6,\\"image\\":\\"https://neigborhood-images.s3.amazonaws.com/property/EBARZA-furniture_0044_Layer_5_2048x2048.jpg\\"},{\\"id\\": 7,\\"image\\":\\"https://neigborhood-images.s3.amazonaws.com/property/EBARZA-furniture_0042_Layer_3_2048x2048.jpg\\"}]"
    }
}
"""

struct Response : Decodable {
    let result: Property
    let success: Bool
}

struct Property: Decodable {
    let name: String
    let description: String
    let propertyType: PropertyType
    let latitude, longitude: String

    let images: ImageJSON
    let threeDModel: URL
}

struct PropertyType: Codable {
    let id: Int
    let name, description: String
}

struct Image : Decodable {
    let id : Int
    let image : URL
}

struct ImageJSON : Decodable {
    let images : [Image]

    init(from decoder : Decoder) throws {
        let container = try decoder.singleValueContainer()
        let imageJSONString = try container.decode(String.self)
        let imageJSONData = Data(imageJSONString.utf8)
        images = try JSONDecoder().decode([Image].self, from: imageJSONData)
    }
}

let data = Data(json.utf8)
do {
    let decoder = JSONDecoder()
    let response = try decoder.decode(Response.self, from: data)
    let images = response.result.images.images
    print(images)
} catch {
    print(error)
}

推荐阅读