Ios 使用Swift解码/解析特定JSON
我在尝试用Swift 4解码这个JSON时遇到了很多麻烦Ios 使用Swift解码/解析特定JSON,ios,json,swift,Ios,Json,Swift,我在尝试用Swift 4解码这个JSON时遇到了很多麻烦 { "Items": [ { "id": 1525680450507, "animal": "bee", "type": "insect", "diet": [ "a", "b", "c" ]
{
"Items": [
{
"id": 1525680450507,
"animal": "bee",
"type": "insect",
"diet": [
"a",
"b",
"c"
]
}
],
"Count": 1,
"ScannedCount": 5
}
这是我试图解码的地方
let decoder = JSONDecoder()
let data = try decoder.decode([Animal].self, from: data)
我创建了一个这样的结构
struct Animal: Codable {
var id: Int
var animal: String
var type: String
var diet: [String]
}
let decoder = JSONDecoder()
let data = try decoder.decode(ItemsResponse.self, from: data)
struct ItemsResponse: Codable {
var Items: [Animal]
var Count: Int
var ScannedCount: Int
}
这不管用。我得到一个错误,上面写着
“应解码数组,但找到了字典。”
所以我想也许我需要这样的东西
struct Animal: Codable {
var id: Int
var animal: String
var type: String
var diet: [String]
}
let decoder = JSONDecoder()
let data = try decoder.decode(ItemsResponse.self, from: data)
struct ItemsResponse: Codable {
var Items: [Animal]
var Count: Int
var ScannedCount: Int
}
但这也不行。现在我明白了
“应解码数组,但找到了字符串/数据。”
我如何制作一个结构来解码这个JSON
let data = try decoder.decode([Animal].self, from: data)
[Animal].self
不正确您可以这样使用它:
struct DataJson: Codable {
let items: [Item]
let count, scannedCount: Int
enum CodingKeys: String, CodingKey {
case items = "Items"
case count = "Count"
case scannedCount = "ScannedCount"
}
}
struct Item: Codable {
let id: Int
let animal, type: String
let diet: [String]
}
// MARK: Convenience initializers
extension DataJson {
init(data: Data) throws {
self = try JSONDecoder().decode(DataJson.self, from: data)
}
func jsonData() throws -> Data {
return try JSONEncoder().encode(self)
}
func jsonString(encoding: String.Encoding = .utf8) throws -> String? {
return String(data: try self.jsonData(), encoding: encoding)
}
}
extension Item {
init(data: Data) throws {
self = try JSONDecoder().decode(Item.self, from: data)
}
func jsonData() throws -> Data {
return try JSONEncoder().encode(self)
}
func jsonString(encoding: String.Encoding = .utf8) throws -> String? {
return String(data: try self.jsonData(), encoding: encoding)
}
}
试试这个:
<代码>导入基础
让json=”“”
{
“项目”:[
{
“id”:1525680450507,
“动物”:“蜜蜂”,
“类型”:“昆虫”,
“饮食”:[
“a”,
“b”,
“c”
]
}
],
“计数”:1,
“扫描计数”:5
}
"""
结构动物:可编码{
变量id:Int
动物:字符串
变量类型:字符串
变量饮食:[字符串]
}
结构项响应:可编码{
变量项:[动物]
变量计数:Int
var ScannedCount:Int
}
让数据=尝试!JSONDecoder().decode(ItemsResponse.self,from:json.data(使用:.utf8)!)
当然,您应该正确处理可能出现的故障(即不要执行try!
,也不要强制打开json.data()!
部分)
但是上面的代码很有效,希望能回答你的问题。通过你最后给出的两条代码,我在操场上找到了它。也许你正在尝试“真正的JSON”(不仅仅是那个示例),还有一个可选值(例如,如果找不到不同类型的值)。你的代码是对的,它在我这边工作,这是我从邮递员那里得到的响应,当我打印出我在Swift中得到的JSON时,它看起来是一样的。但我还是会出错。完全错误是这种错误类型不匹配(Swift.Array,Swift.DecodingError.Context(codingPath:[CodingKeys(stringValue:“Items”,intValue:nil),_JSONKey(stringValue:“Index 0”,intValue:0),CodingKeys(stringValue:“diet”,intValue:nil)],debugDescription:“应该解码数组,但找到了字符串/数据。”,underyingerror:nil))哦,这是因为在Items数组中,我有其他没有diet作为字符串数组的对象,它只是一个字符串。我只贴了一个例子。一旦我纠正了这一点,一切都正常了。谢谢你确认我的代码有效。