首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在响应中解析简单的json字符串值

在响应中解析简单的json字符串值
EN

Stack Overflow用户
提问于 2019-07-04 06:04:12
回答 3查看 891关注 0票数 1

我想解析这个响应。其他一切都会被解析--除了图像。我收到它的字符串,但我不能把它转换成字典。这是我的模型。

代码语言:javascript
复制
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响应:

代码语言:javascript
复制
        "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

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2019-07-04 07:27:24

创建如下所示的图像类型

代码语言:javascript
复制
struct PropertyImage: Decodable {
    var id: Int
    var image: String?
}

现在,从data字符串中获取imagesdecode属性图像的array,如下所示,

代码语言:javascript
复制
if let data = property.images.data(using: .utf8) {
    do {
        let images = try JSONDecoder().decode([PropertyImage].self, from: data)
        images.forEach { image in
           print(image.id)
           print(image.image)
         }

    } catch {
        print(error)
    }
}
票数 1
EN

Stack Overflow用户

发布于 2019-07-04 07:38:31

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

一个解决方案是使用一个ImageJSON将包含嵌套JSON字符串的值声明为struct ( singleValueContainer ),并在第二级对该字符串进行解码。

我忽略了JSON中没有的属性

代码语言:javascript
复制
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\\"}]"
    }
}
"""
代码语言:javascript
复制
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)
    }
}
代码语言:javascript
复制
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)
}
票数 6
EN

Stack Overflow用户

发布于 2019-07-04 06:39:31

试试下面的代码:

更新您的json:

代码语言:javascript
复制
let json = """
{
"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"}
]
}
"""

更新您的可编码类:

代码语言:javascript
复制
struct Property : Codable {

    let descriptionField : String?
    let images : [Images]?
    let latitude : String?
    let longitude : String?
    let name : String?
    let propertyType : PropertyType?
    let threeDModel : String?
    let viewOfWater : String?

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


struct PropertyType : Codable {

    let descriptionField : String?
    let id : Int?
    let name : String?

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


struct Images : Codable {
    let id : Int?
    let image : String?
}




if let jsonData = json.data(using: .utf8) {
    let decoder = JSONDecoder()

    do {
        let jsonModel = try decoder.decode(Property.self, from: jsonData)
        print(jsonModel)
    } catch {
        print("Error = \(error)")
    }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56881536

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档