ios - 将 json 文件导入对象错误
问题描述
我有这个 json 文件: http ://serwer1356363.home.pl/pub/json.txt
这个代码:
var ProductsObjectArray = [Products]()
let data = try Data(contentsOf: path)
let decoder = JSONDecoder()
let ProductsObjectArray = try decoder.decode(Products.self, from: data)
和型号:
struct ProductObject : Codable {
let palletHeight : Double?
let layerPallet : Int?
let prepCombisteamer : String?
let id : Int?
let prepOven : String?
}
当我启动这段代码时,我得到一个错误:
Swift.DecodingError.typeMismatch(Swift.Dictionary, Swift.DecodingError.Context(codingPath: [], debugDescription: "期望解码 Dictionary 但找到了一个数组。",底层错误: nil))
我想将 json 写入 Products Object Array 对象数组。有人怎么修呢?
解决方案
问题是Dictionary
当 JSON 是Array
改变
let ProductsObjectArray = try decoder.decode(Products.self, from: data)
到
let ProductsObjectArray = try decoder.decode([Products].self, from: data)
推荐阅读
- google-cloud-platform - Anthos Service Mesh/Istio CORS 未启用
- sql - SQL Server 解析 JSON 以更新另一个表
- python - 保存后实际图形大小发生变化
- javascript - 记录 EventEmitters 的正确方法是什么?
- c# - C# 以错误的顺序向 Microsoft 团队发送击键
- c# - xamarin android绑定事件
- r - 在 R 中应用涉及两个数据框列的 if else 语句
- java - 为什么当我尝试重新分配一个变量时我收到错误,“删除这个无用的分配给局部变量”
- kotlin - Kotlin 检查 Collection 是否既不为空也不为空
- swift - 如何在 Swift 的 UICollectionView 中一行显示 2 个单元格?