gpt4 book ai didi

ios - 将对象保存到 json 文件

转载 作者:可可西里 更新时间:2023-11-01 01:08:49 27 4
gpt4 key购买 nike

我有要写入 json 文件的对象。

我的代码:

struct ChangeTime : Codable {
let second : Int?
let year : Int?
let month : Int?
let hourOfDay : Int?
let dayOfMonth : Int?
let minute : Int?
}

struct Inspirations : Codable {
let avikoSegmentyRynku : [String]?
let id : Int?
let code : String?
let avikoOkazjeJedzenia : [String]?
let peopleCount : String?
let productCodes : String?
let langVersions : [LangVersions]?
}

struct InspiracjeProdukty : Codable { // main
let products : [Products]?
let inspirations : [Inspirations]?
}

struct Lang : Codable {
let pL : String?
let name : String?
let active : Bool?
let id : Int?
let code : String?
}


struct LangVersions : Codable {
let ingredients : String?
let parentId : Int?
let name : String?
let preparation : String?
let id : Int?
let advice : String?
let lang : Lang?
let active : Bool?
let dostepnaMobilnie : Bool?
let favorite : Bool?
}

struct Products : Codable {
let prepDeepFryer : String?
let shelfLife : String?
let avikoPodSegmentyRynku : [String]?
let fat : Double?
let markedAsFavourite1 : Bool?
let kcal : Int?
let shelfLifeTimeFrame : String?
let active : Bool?
let langVersions : [LangVersions]?
let changeTime : ChangeTime?
let prepMicrowave : String?
let proteins : Double?
let prepCombisteamer : String?
let id : Int?
let inPieces : Bool?
let code : String?
let eanBox : String?
let containsGluten : Bool?
let packageContents : Double?
let layerPallet : Int?
let palletWeight : Double?
let name : String?
let avikoConcepts : [String]?
let kj : Int?
let noBox : Int?
let markedAsFavourite3 : Bool?
let sunFlower : Bool?
let eanFoil : String?
let weightYieldPercent : Int?
let avikoSegmentyRynku : [String]?
let carbohydrates : Double?
let prepPot : String?
let markedAsFavourite2 : Bool?
let palletHeight : Double?
let contentPerBox : Int?
let avikoWorlds : [String]?
let avikoSegments : [String]?
let regions : [Int]?
let prepOven : String?
let prepFryingPan : String?
let boxLayer : Int?
}

func checkProductsStartFiles(path: URL) {
guard let gitUrl = URL(string: path.absoluteString) else { return }
URLSession.shared.dataTask(with: gitUrl) { (data, response
, error) in
guard let data = data else { return }
do {
let decoder = JSONDecoder()
let jsonData = try decoder.decode(InspiracjeProdukty.self, from: data)
//print(jsonData)
if (jsonData.products?.count)! > 0 && (jsonData.inspirations?.count)! > 0 {
self.saveTempToFinnalFiles(jsonData: jsonData)
}
} catch let err {
self.errorLoginMessage(txt: "\(ErrorsLabels.MainViewControler03). \(err)", title: "Blad".localized())
}
}.resume()
}

func saveTempToFinnalFiles(jsonData: InspiracjeProdukty){
do {
let productsToFile = try JSONSerialization.jsonObject(with: jsonData.products) as? [String: Any]
let inspirationsToFile = try JSONSerialization.jsonObject(with: jsonData.inspirations) as? [String: Any]

let documentsDir = FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask).first!

try productsToFile.write(to: documentsDir.appendingPathComponent(selectedLanguage + "/json/products.json"))
try inspirationsToFile.write(to: documentsDir.appendingPathComponent(selectedLanguage + "/json/inspirations.json"))

print("XXXX")
debugPrint(jsonData)
//removeFile(path: path)
//EZLoadingActivity.hide()
} catch {
print("Error deserializing JSON: \(error)")
}
}

在函数中:saveTempToFinnalFiles - 我想将这 2 个带有对象的表保存到单独的文件中:products.jsoninspirations.json .在行中:

let productsToFile = try JSONSerialization.jsonObject (with: jsonData.products) as? [String: Any]
let inspirationsToFile = try JSONSerialization.jsonObject (with: jsonData.inspirations) as? [String: Any]

我收到一个错误:

Can not invoke 'jsonObject' with an argument list of type '(with: [Products]?)' / Cannot invoke 'jsonObject' with an argument list of type '(with: [Inspirations]?)'

有人知道怎么解决吗?

最佳答案

试试这段代码

func saveTempToFinnalFiles(jsonData: InspiracjeProdukty){
do {
let productsToFile = try JSONEncoder().encode(jsonData.products)

let inspirationsToFile = try JSONEncoder().encode(jsonData.inspirations)

let documentsDir = FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask).first!

try productsToFile.write(to: documentsDir.appendingPathComponent(selectedLanguage + "/json/products.json"))
try inspirationsToFile.write(to: documentsDir.appendingPathComponent(selectedLanguage + "/json/inspirations.json"))

print("XXXX")
debugPrint(jsonData)
//removeFile(path: path)
//EZLoadingActivity.hide()
} catch {
print("Error deserializing JSON: \(error)")
}
}

关于ios - 将对象保存到 json 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50461984/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com