首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >typeMismatch(Swift.Array<Any>,Swift.DecodingError.Context(codingPath:[CodingKeys(stringValue:"product",intValue: nil)]

typeMismatch(Swift.Array<Any>,Swift.DecodingError.Context(codingPath:[CodingKeys(stringValue:"product",intValue: nil)]
EN

Stack Overflow用户
提问于 2022-03-07 19:31:42
回答 1查看 290关注 0票数 0

我已经有很长一段时间没有解决这个问题了。我正在尝试从URL响应中解码JSON。我试过很多事情,比如改变解码器。到这个

代码语言:javascript
运行
复制
let productData = try JSONDecoder().decode([ProductDetail].self, from: data)

我得到的错误是

代码语言:javascript
运行
复制
typeMismatch(Swift.Array<Any>, Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "product", intValue: nil)], debugDescription: "Expected to decode Array<Any> but found a dictionary instead.", underlyingError: nil))
typeMismatch(Swift.Array<Any>, Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "product", intValue: nil)], debugDescription: "Expected to decode Array<Any> but found a dictionary instead.", underlyingError: nil))
typeMismatch(Swift.Array<Any>, Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "product", intValue: nil)], debugDescription: "Expected to decode Array<Any> but found a dictionary instead.", underlyingError: nil))
typeMismatch(Swift.Array<Any>, Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "product", intValue: nil)], debugDescription: "Expected to decode Array<Any> but found a dictionary instead.", underlyingError: nil))

这是我的产品结构

代码语言:javascript
运行
复制
struct ProductDetail: Codable {
    var code: String?
    var product: [Product?]
}
struct Product: Codable {
    var _id: String?
    var additives_tags: [String]? // [JSON?]
    var allergens_tags: [String]?// [JSON?]
    var brand_owner: String?
    var countries_tags: [String]?  // [JSON?]
    var image_url: String?
    var image_front_small_url: String?
    var image_front_thumb_url: String?
    var image_front_url: String?
    var image_ingredients_small_url: String?
    var image_ingredients_thumb_url: String?
    var image_ingredients_url: String?
    
}

这是提取函数

代码语言:javascript
运行
复制
func fetchProduct(completionHandler: @escaping (ProductDetail) -> Void){
        let url = URL(string: "https://world.openfoodfacts.org/api/v0/product/87222241")!
        
        URLSession.shared.dataTask(with: url) { (data,
        response, error) in
            guard let data = data else { return}
            
            do {
                let productData = try JSONDecoder().decode(ProductDetail.self, from: data)
                print(productData)
                completionHandler(productData)
            }catch {
                let error = error
                print(error)
            }
            
        }.resume()
        
    }

json文件(https://world.openfoodfacts.org/api/v0/product/87222241)

代码语言:javascript
运行
复制
{
    "code": "87222241",
    "product": {
        "_id": "87222241",
        "_keywords": [
            "aa",
            "drink"
        ],
        "added_countries_tags": [],
        "additives_debug_tags": [],
        "additives_old_tags": [],
        "additives_original_tags": [],
        "additives_prev_original_tags": [],
        "additives_tags": [],
        "allergens": "",
        "allergens_from_ingredients": "",
        "allergens_from_user": "(fr) ",
        "allergens_hierarchy": [],
        "allergens_tags": [],
        "amino_acids_prev_tags": [],
        "amino_acids_tags": [],
        "brands": "AA drink ",
        "brands_tags": [
            "aa-drink"
        ],
        "categories_debug_tags": [],
        "categories_hierarchy": [],
        "categories_prev_hierarchy": [],
        "categories_prev_tags": [],
        "categories_properties": {},
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-03-07 19:36:05

您已经将product定义为模型中的数组。但是,正如错误说的那样,在JSON中,它是一个Dictionary。将模型更改为:

代码语言:javascript
运行
复制
struct ProductDetail: Codable {
    var code: String?
    var product: Product? //<-- Here
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71386328

复制
相关文章

相似问题

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