Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何解析JSON?敏捷的_Json_Swift - Fatal编程技术网

如何解析JSON?敏捷的

如何解析JSON?敏捷的,json,swift,Json,Swift,口袋妖怪 下面是口袋妖怪的结构。我想停止数组中的所有口袋妖怪数据 进口基金会 导入UIKit struct Pokemon: Codable { let name: String let type: String let imageUrl: String let description: String let weight: Int let height: Int let defense: Int let attack: Int }

口袋妖怪 下面是口袋妖怪的结构。我想停止数组中的所有口袋妖怪数据 进口基金会 导入UIKit

struct Pokemon: Codable {
    let name: String
    let type: String
    let imageUrl: String
    let description: String
    let weight: Int
    let height: Int
    let defense: Int
    let attack: Int
}
API获取函数

func fetchingPokemonfromAPI() -> Void) {
        
        guard let url = URL(string: "https://pokedex-bb36f.firebaseio.com/pokemon.json") else {return}
        
        URLSession.shared.dataTask(with: url) { (data, _, error) in
            
            guard let data = data?.datafilter(removeString: "null,"), error == nil else {return}
                print("we have data")
            

                do {
                    let jsondata = try JSONDecoder().decode([Pokemon].self, from: data)
                        print(jsondata.attack)
                }
                catch {
                    print("uable to process data in Catch block")
                
            }
        }.resume()
    }
}

extension Data {
    func datafilter(removeString string: String) -> Data? {
        let dataString = String(data:self, encoding: .utf8)
        let parsedData = dataString?.replacingOccurrences(of: string, with: "")
        guard let data = parsedData?.data(using: .utf8) else { return nil }
        return data
    }
}
错误-> 我们有数据
可以处理Catch块中的数据

此API不会返回
[Pokemon]
。它返回一个字典,其中除最后一个元素外,所有元素都有Pokemon值,但最后一个元素有一些关于响应的元数据。您可以通过获取所有值并尝试将其解码为口袋妖怪,并保留有效值来解码:

// This is needed to decode arbitrary keys
struct AnyStringKey: CodingKey, Hashable {
    var stringValue: String
    init(stringValue: String) { self.stringValue = stringValue }
    init(_ stringValue: String) { self.init(stringValue: stringValue) }
    var intValue: Int?
    init?(intValue: Int) { return nil }
}

struct PokedexResponse: Decodable {
    var pokemon: [Pokemon]

    init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: AnyStringKey.self)
        self.pokemon = container.allKeys.compactMap { try? container.decode(Pokemon.self, forKey: $0) }
    }
}


let jsondata = try JSONDecoder().decode(PokedexResponse.self, from: data).pokemon

显然,您正在跳过进化链和id。仍然不起作用。donno我在这里做错了什么请打印错误对象而不是您自己的字符串。上面的代码无法编译,或者无法演示您看到的问题。请注意,您链接的数据不是
[Pokemon]
,而是
[String:Pokemon]
。如果您在catch中打印
error
,它还将告诉您它发现了什么问题。可以处理catch块keyNotFound(CodingKeys(stringValue:“name”,intValue:nil)、Swift.DecodingError.Context(codingPath:[],debugDescription:“没有与键CodingKeys关联的值(stringValue:“name\”,intValue:nil)(“name\”),参考误差:零)