[英]typeMismatch(Swift.Dictionary<Swift.String, Any>,
my JSON:我的 JSON:
'''
{
"status": 200,
"data": {
"widgets": [
{
"type": "small_half_slider",
"data": {
"banners": [
{
"id": 42765,
"image": "",
"title": "!",
"is_ad": false,
"url": {
"url": "",
"page": "web_link",
"params": {
"url": ""
},
"queries": []
},
"video": "",
"description": "",
"bullet_points": []
}
'''
and my Struct:和我的结构:
'''
import Foundation
struct mainPage:Codable{
var status:Int?
var data: mData?
}
struct mData:Codable {
var widgets:[Mwidg]
}
struct Mwidg:Codable{
var type:String
var data:wdata
}
struct wdata:Codable{
var show_type:String?
var banners:[bannerData]?
}
struct bannerData:Codable {
var id:Int?
var image:String?
var title:String?
var is_ad:Bool?
var url:urlData?
var video:String?
var description:String?
}
struct urlData:Codable {
var url:String?
var page:String?
var params:paramsData?
var queries:[queriesData]?
}
struct paramsData:Codable {
var url:String?
}
struct queriesData:Codable {
}
'''
then i faced to this error:然后我遇到了这个错误:
typeMismatch(Swift.Dictionary<Swift.String, Any>, Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "data", intValue: nil), CodingKeys(stringValue: "widgets", intValue: nil), _JSONKey(stringValue: "Index 1", intValue: 1), CodingKeys(stringValue: "data", intValue: nil)], debugDescription: "Expected to decode Dictionary<String, Any> but found an array instead.", underlyingError: nil)) typeMismatch(Swift.Dictionary<Swift.String, Any>, Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "data", intValue: nil), CodingKeys(stringValue: "widgets", intValue: nil), _JSONKey( stringValue: "Index 1", intValue: 1), CodingKeys(stringValue: "data", intValue: nil)], debugDescription: "期望解码 Dictionary<String, Any> 但找到了一个数组。",underlyingError: nil) )
You are facing this issue because in your json response you are not getting values or getting nil values for properties which are present in struct mData and Mwidg, to work your solution make struct properties as optional so that even if you didn't receive data in your json response the code will work.您正面临这个问题,因为在您的 json 响应中,您没有获取值或获取 struct mData 和 Mwidg 中存在的属性的 nil 值,以便您的解决方案将结构属性设为可选,这样即使您没有收到数据您的 json 响应代码将起作用。 The json you pasted in question is not completed and it may possible that you were getting some object as nil.
您粘贴的 json 尚未完成,您可能会得到一些 object 为零。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.