Ios 获取和解析JSON

Ios 获取和解析JSON,ios,json,swift,parsing,decodable,Ios,Json,Swift,Parsing,Decodable,我很难获取和解析以下JSON。我甚至不能从给定的url获取数据,更不用说使用我的数据模型“Car”解析它了。欢迎任何帮助 JSON 您的汽车模型并不代表您的json结构 您正在寻找这样的东西: struct CarsResponse: Decodable { let cars: [CarDTO] } struct CarDTO: Decodable { let id: String let isStockImg: Bool // And so on } 然后写

我很难获取和解析以下JSON。我甚至不能从给定的url获取数据,更不用说使用我的数据模型“Car”解析它了。欢迎任何帮助

JSON


您的汽车模型并不代表您的json结构

您正在寻找这样的东西:

struct CarsResponse: Decodable {
    let cars: [CarDTO]
}

struct CarDTO: Decodable {
    let id: String
    let isStockImg: Bool
    // And so on
}
然后写下:

let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
let carResponse = try decoder.decode(CarsResponse.self, from: data)
您将通过书写访问您的汽车

let cars: [CarDTO] = carResponse.cars

如果要使用Codable协议和
JSONDecoder()
,则需要创建一个与JSON格式匹配的结构。正如其他人在评论中指出的,您的汽车结构与您接收的数据格式完全不同

如果您只想将接收到的JSON反序列化到包含值的字典中,那么可以使用JSONSerialization

为此,您可以使用如下代码替换
do
块的主体:

let object = try JSONSerialization.jsonObject(with: data, options: []) 
然后,您必须自己导航
对象的字典结构。它不像创建自定义结构和使用Codable那样干净,但它可以工作

请注意,JSON中有语法错误。以下是一个已清理的版本:

{"cars":
    [
        {"date_stolen":1604616183,
            "description":null,
            "body_colors":["Black","Blue"],
            "id":944846,
            "is_stock_img":false,
            "large_img":null,
            "location_found":null,
            "manufacturer_name":"Toyota",
            "external_id":null,
            "registry_name":null,
            "registry_url":null,
            "serial":"36-17-01012-xl09",
            "status":null,
            "stolen":true,
            "stolen_location":"Calgary - CA",
            "thumb":null,
            "title":"2017 Toyota Corolla ",
            "url":"https://cars.org/944846",
            "year":2017
        }
    ]
}

(为了可读性,我添加了空格。它不会以这种或那种方式影响解析。)

输出是什么?您的数据模型car不反映json结构。因为
cars
不是字符串,所以它不会解析您的json格式不正确。它有不匹配的引号、不匹配的“[”和不匹配的“{”字符。谢谢你,伙计。这似乎有效。由于类型不匹配,我不得不将“self.cars=try decoder.decode(Car.self,from:data)”替换为“let data=try decoder.decode(Car.self,from:data)”。现在需要弄清楚如何提取响应。
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
let carResponse = try decoder.decode(CarsResponse.self, from: data)
let cars: [CarDTO] = carResponse.cars
let object = try JSONSerialization.jsonObject(with: data, options: []) 
{"cars":
    [
        {"date_stolen":1604616183,
            "description":null,
            "body_colors":["Black","Blue"],
            "id":944846,
            "is_stock_img":false,
            "large_img":null,
            "location_found":null,
            "manufacturer_name":"Toyota",
            "external_id":null,
            "registry_name":null,
            "registry_url":null,
            "serial":"36-17-01012-xl09",
            "status":null,
            "stolen":true,
            "stolen_location":"Calgary - CA",
            "thumb":null,
            "title":"2017 Toyota Corolla ",
            "url":"https://cars.org/944846",
            "year":2017
        }
    ]
}