gpt4 book ai didi

ios - 如何在 Swift 模型中存储 JSON 响应

转载 作者:行者123 更新时间:2023-11-29 05:56:47 32 4
gpt4 key购买 nike

我想在tableview中显示汽车数据,并且有两种类型的历史模型。正如您在我的回复中看到的,汽车的第一个对象有历史记录,其他对象没有历史记录。如何在 VehicalModel 结构中制作历史结构 json 模型,如何在 alamofire 的帮助下访问该模型和 map 。以及如何检查历史记录是否可用(如果可用)然后存储在模型中并显示在表格 View 中。

这是我的回复

{
"response": "success",
"account_type": "2",
"car_data": [
{
"registration_no": "Lzq 2233",
"engincc": "600 - 999",
"enginccID": "1",
"vehicleID": "32",
"history": [
{
"packages": "",
"date_time": "2018-12-22 00:40:55",
"bill_amount": "7098",
"bill_discount": "133.0571251",
"bill_paid": "36070"
}
]
},
{
"registration_no": "ghfdhhh",
"engincc": "1500 - 1799",
"enginccID": "3",
"vehicleID": "33",
"history": "None"
}
]
}

这是我的模型

struct VehicleDataModel {
var registrationNo : String?
var engineCC: String?
var engineCCID: String?
var vehicleID: String?
var history: [HistoryModel]


struct HistoryModel {
var packages: String
var billDiscount : String
var dateTime: String
var billPaid: String
var billAmount: String
}
}

这是我的调用 API 函数:

func callApi() {
let url = "http://esspk.net/production/20m/Api/getVehicleApi"
let userID = UserDefaults.standard.integer(forKey: "user_id")
let param = ["user_id" : userID]
print(param)
ServerCall.makeCallWitoutFile(url, params: param, type: Method.POST, currentView: nil) { (response) in
if let json = response {
print(json)
if let carData = json["car_data"].array
{
//let vehicalObj = VehicleDataModel()

for cData in carData {

let regNo = cData["registration_no"].string
let enginCC = cData["engincc"].string
let enginID = cData["enginccID"].string
let vehicleID = cData["vehicleID"].string

let history = cData["history"].arrayObject
// let vech = VehicleDataModel(registrationNo: regNo, engineCC: enginCC, engineCCID: enginID, vehicleID: vehicle, history: history)
// self.vehicalModel.append(vech)
// let vech = VehicleDataModel.init(registrationNo: regNo, engineCC: enginCC, engineCCID: enginID, vehicleID: vehicleID, history: VehicleDataModel.HistoryModel( )
}
self.myVehicleTblView.reloadData()
}
}
}
}

最佳答案

第一个制作模型类。

class Welcome: Codable {
let response, accountType: String
let carData: [CarDatum]

enum CodingKeys: String, CodingKey {
case response
case accountType = "account_type"
case carData = "car_data"
}

init(response: String, accountType: String, carData: [CarDatum]) {
self.response = response
self.accountType = accountType
self.carData = carData
}
}

class CarDatum: Codable {
let registrationNo, engincc, enginccID, vehicleID: String
let history: HistoryUnion

enum CodingKeys: String, CodingKey {
case registrationNo = "registration_no"
case engincc, enginccID, vehicleID, history
}

init(registrationNo: String, engincc: String, enginccID: String, vehicleID: String, history: HistoryUnion) {
self.registrationNo = registrationNo
self.engincc = engincc
self.enginccID = enginccID
self.vehicleID = vehicleID
self.history = history
}
}

enum HistoryUnion: Codable {
case historyElementArray([HistoryElement])
case string(String)

init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
if let x = try? container.decode([HistoryElement].self) {
self = .historyElementArray(x)
return
}
if let x = try? container.decode(String.self) {
self = .string(x)
return
}
throw DecodingError.typeMismatch(HistoryUnion.self, DecodingError.Context(codingPath: decoder.codingPath, debugDescription: "Wrong type for HistoryUnion"))
}

func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
switch self {
case .historyElementArray(let x):
try container.encode(x)
case .string(let x):
try container.encode(x)
}
}
}

class HistoryElement: Codable {
let packages, dateTime, billAmount, billDiscount: String
let billPaid: String

enum CodingKeys: String, CodingKey {
case packages
case dateTime = "date_time"
case billAmount = "bill_amount"
case billDiscount = "bill_discount"
case billPaid = "bill_paid"
}

init(packages: String, dateTime: String, billAmount: String, billDiscount: String, billPaid: String) {
self.packages = packages
self.dateTime = dateTime
self.billAmount = billAmount
self.billDiscount = billDiscount
self.billPaid = billPaid
}
}

或者你也可以使用下面的类

struct Welcome {
let response, accountType: String
let carData: [CarDatum]
}

struct CarDatum {
let registrationNo, engincc, enginccID, vehicleID: String
let history: HistoryUnion
}

enum HistoryUnion {
case historyElementArray([HistoryElement])
case string(String)
}

struct HistoryElement {
let packages, dateTime, billAmount, billDiscount: String
let billPaid: String
}

然后我推荐使用Alamofire来调用API。

func request() {

let url = URL(string: "my url")
Alamofire.request(url!).responseJSON {(response) in

switch (response.result) {
case .success:
if let data = response.data {
do {
let response = try JSONDecoder().decode([Welcome].self, from: data)
self.arrList = response
DispatchQueue.main.async {
//Print Responce
}
} catch {
print(error.localizedDescription)
}
}
case .failure( let error):
print(error)
}
}
}
}

希望这有效。

关于ios - 如何在 Swift 模型中存储 JSON 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55075219/

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