将本地 json 文件解析为对象
Parse local json file to object
我有这个代码:
struct ProductObject : Codable {
let palletHeight : Double?
let layerPallet : Int?
let prepCombisteamer : String?
let id : Int?
let avikoWorlds : [String]?
let avikoSegments : [String]?
let sunFlower : Bool?
let inPieces : Bool?
let noBox : Int?
let prepFryingPan : String?
let packageContents : Double?
let carbohydrates : Int?
let fat : Double?
let eanBox : Int?
let weightYieldPercent : Int?
let kcal : Int?
let markedAsFavourite1 : Bool?
let avikoPodSegmentyRynku : [String]?
let prepPot : String?
let prepMicrowave : String?
let name : String?
let code : Int?
let prepDeepFryer : String?
let avikoConcepts : [String]?
let boxLayer : Int?
let avikoSegmentyRynku : [String]?
let active : Bool?
let shelfLifeTimeFrame : String?
let markedAsFavourite2 : Bool?
let palletWeight : Double?
let changeTime : ChangeTime?
let kj : Int?
let langVersions : [LangVersions]?
let proteins : Double?
let markedAsFavourite3 : Bool?
let containsGluten : Bool?
let regions : [Int]?
let eanFoil : Int?
let shelfLife : Int?
let contentPerBox : Int?
let prepOven : String?
}
func downloadImagesFromJsonProductFile(fileName: URL){
let filePath = fileName.path
let fileManager = FileManager.default
if fileManager.fileExists(atPath: filePath) {
do {
let data = try Data(contentsOf: fileName)
let jsonData = try JSONDecoder().decode(ProductObject.self, from: data)
} catch let error {
self.errorLoginMessage(txt: "MainView - Error 109: Problem with parse file \(error)", title: "Blad".localized())
}
}
}
downloadImagesFromJsonProductFile(fileName: documentsDir.appendingPathComponent((AppGlobalManager.sharedManager.loggedUser?.selectedLanguage)! + "/json/products.json"))
我的本地 json 文件:https://files.fm/u/73n845ty
当我 运行 函数:downloadImagesFromJsonProductFile 我有错误
(应用程序启动时):解析文件类型不匹配问题(Swift.Dictionary,Swift.DecodingError.Context(编码路径:[],调试描述:"Expected the decode Dictionary but found an array instead.",底层错误:will)。
如何解决?
当我在这里 http://json.parser.online.fr/ 查看你的 JSON 时,有数组作为根对象,因此你应该
let json = try JSONDecoder().decode([ProductObject].self, from: data)
更新
请从此处创建 JSON struct Codable
json4swift,请先解决类型不匹配错误,否则无法解析您的 JSON,您将收到类型不匹配错误。
您可以通过以下方式获取产品代码,
let jsonData = try JSONDecoder().decode([ProductObject].self, from: data)
for detail in jsonData {
print(detail.code ?? "")
}
我有这个代码:
struct ProductObject : Codable {
let palletHeight : Double?
let layerPallet : Int?
let prepCombisteamer : String?
let id : Int?
let avikoWorlds : [String]?
let avikoSegments : [String]?
let sunFlower : Bool?
let inPieces : Bool?
let noBox : Int?
let prepFryingPan : String?
let packageContents : Double?
let carbohydrates : Int?
let fat : Double?
let eanBox : Int?
let weightYieldPercent : Int?
let kcal : Int?
let markedAsFavourite1 : Bool?
let avikoPodSegmentyRynku : [String]?
let prepPot : String?
let prepMicrowave : String?
let name : String?
let code : Int?
let prepDeepFryer : String?
let avikoConcepts : [String]?
let boxLayer : Int?
let avikoSegmentyRynku : [String]?
let active : Bool?
let shelfLifeTimeFrame : String?
let markedAsFavourite2 : Bool?
let palletWeight : Double?
let changeTime : ChangeTime?
let kj : Int?
let langVersions : [LangVersions]?
let proteins : Double?
let markedAsFavourite3 : Bool?
let containsGluten : Bool?
let regions : [Int]?
let eanFoil : Int?
let shelfLife : Int?
let contentPerBox : Int?
let prepOven : String?
}
func downloadImagesFromJsonProductFile(fileName: URL){
let filePath = fileName.path
let fileManager = FileManager.default
if fileManager.fileExists(atPath: filePath) {
do {
let data = try Data(contentsOf: fileName)
let jsonData = try JSONDecoder().decode(ProductObject.self, from: data)
} catch let error {
self.errorLoginMessage(txt: "MainView - Error 109: Problem with parse file \(error)", title: "Blad".localized())
}
}
}
downloadImagesFromJsonProductFile(fileName: documentsDir.appendingPathComponent((AppGlobalManager.sharedManager.loggedUser?.selectedLanguage)! + "/json/products.json"))
我的本地 json 文件:https://files.fm/u/73n845ty
当我 运行 函数:downloadImagesFromJsonProductFile 我有错误 (应用程序启动时):解析文件类型不匹配问题(Swift.Dictionary,Swift.DecodingError.Context(编码路径:[],调试描述:"Expected the decode Dictionary but found an array instead.",底层错误:will)。
如何解决?
当我在这里 http://json.parser.online.fr/ 查看你的 JSON 时,有数组作为根对象,因此你应该
let json = try JSONDecoder().decode([ProductObject].self, from: data)
更新
请从此处创建 JSON struct Codable
json4swift,请先解决类型不匹配错误,否则无法解析您的 JSON,您将收到类型不匹配错误。
您可以通过以下方式获取产品代码,
let jsonData = try JSONDecoder().decode([ProductObject].self, from: data)
for detail in jsonData {
print(detail.code ?? "")
}