Swift 4 无法读取数据,因为它的格式不正确

Geo*_*nts 9 parsing json ios swift

我正在开发一个项目来学习如何解析 JSON。我正在尝试将JSON解析为struct。我正在尝试使用接下来的代码来执行此操作,但出现以下错误:

Err 无法读取数据,因为它的格式不正确。

我究竟做错了什么?我也尝试使用 Alamofire,但我没有找到将它解析为 struct 的方法。

func getData(){
    let gitUrl = URL(string: "http://95.46.99.250:9095/api/v1/institution-categories")
    URLSession.shared.dataTask(with: gitUrl!) { (data, response
        , error) in
        let data = data
        print(data)
        do {
            let decoder = JSONDecoder()
            let gitData = try decoder.decode([Root].self, from: data!)

        } catch let err {
            print("\nErr", err.localizedDescription)
        }
        }.resume()
}
Run Code Online (Sandbox Code Playgroud)

结构

struct Root: Codable {
    let  data: [InnerItem]
}
struct InnerItem:Codable {
    let  id: Int?
    let  image: String?
    let  name: String?

    private enum CodingKeys : String, CodingKey {
        case id = "id", image = "image", name = "name"
    }
}
Run Code Online (Sandbox Code Playgroud)

JSON

{
"data": [
    {
        "id": 1,
        "name": "???? ? ????",
        "image": "http://95.46.99.250:9095/storage/photos/beer@2x.png"
    },
    {
        "id": 2,
        "name": "????",
        "image": "http://95.46.99.250:9095/storage/photos/coffee@3x.png"
    },
    {
        "id": 3,
        "name": "?????? ????",
        "image": "http://95.46.99.250:9095/storage/photos/0201f7523bc2028f174710b51379e432.png"
    },
    {
        "id": 4,
        "name": "????????",
        "image": "http://95.46.99.250:9095/storage/photos/restaurants@3x.png"
    },
    {
        "id": 5,
        "name": "???????-????",
        "image": "http://95.46.99.250:9095/storage/photos/microphone.png"
    },
    {
        "id": 6,
        "name": "????-???",
        "image": "http://95.46.99.250:9095/storage/photos/sushi.png"
    },
    {
        "id": 7,
        "name": "????????",
        "image": "http://95.46.99.250:9095/storage/photos/pizza.png"
    },
    {
        "id": 8,
        "name": "?????????",
        "image": "http://95.46.99.250:9095/storage/photos/c111d1e5ad6b90b61ac36836d220ebba.png"
    },
    {
        "id": 9,
        "name": "?????",
        "image": "http://95.46.99.250:9095/storage/photos/Group 315@3x.png"
    }
]
}
Run Code Online (Sandbox Code Playgroud)

Moj*_*ini 12

故障coding/decoding错误

使用codables 时,不要打印 ,而是.localizedDescription尝试打印出它error本身!所以编译器描述了问题到底在哪里!

do {
    let decoder = JSONDecoder()
    let decoded = try decoder.decode([Root].self, from: data!)
} catch {
    // print(localizedDescription) // <- ?? Don't use this!

    print(String(describing: error)) // <- ? Use this for debuging!
}
Run Code Online (Sandbox Code Playgroud)

在你的情况下

它将指出:

  • 解码器尝试将根对象解码为 anArray但找到了 a Dictionary

所以你关注这个问题,看到你应该替换:

decoder.decode([Root].self, from: data!)
Run Code Online (Sandbox Code Playgroud)

和:

decoder.decode(Root.self, from: data!)
Run Code Online (Sandbox Code Playgroud)

  • print(String(describing: error)) // &lt;- ✅ 使用它进行调试!谢谢伙计,这条线真的很有帮助。 (9认同)

Rat*_*ker 8

尝试这个

let gitData = try decoder.decode(Root.self, from: data!)
Run Code Online (Sandbox Code Playgroud)

遍历您的数据

for singleData in gitData.data where (singleData.name ?? "") == "Cafe" {
    print(singleData.image)
}
Run Code Online (Sandbox Code Playgroud)