gpt4 book ai didi

ios - 如何基于json构造struct

转载 作者:行者123 更新时间:2023-11-28 19:26:35 24 4
gpt4 key购买 nike

我根据我创建的结构列出了 Json 结果,但在解析时出现错误类型不匹配。在代码中创建结构时,我无法找到问题所在

我创建了基于 json 的结构,它对我不起作用

["status": "1",
"error": false,
"data": [
{
"order_id": "11",
"order_unique_id": "ORDR-1001",
"store_name": "24X7",
"otp_store": "781103",
"product": [
"Product One",
"Product Two"
],
"qty": [
"1",
"3"
],
"user_details": {
"name": "test",
"number": "98145314586",
"landmark": "test",
"area": "Bow East",
"pincode": "E3 9EG",
"place": "Home"
},
"status": "2",
"date": "2018-12-13",
"time": "14:37:57"
}]


struct RunnerStatus: Decodable {
var status: String
var error: Bool
var data: [Runner]
}

struct Runner: Decodable {
var order_id: String
var order_unique_id: String
var store_name: String
var otp_store: String
var product: [Product]
var qty: [Quantity]
var user_details: [UserDetails]
var status: String
var date: String
var time: String
}

struct Product: Decodable {
var ProductName: String
}

struct Quantity: Decodable {
var qty: String
}

struct UserDetails: Decodable {
var name: String
var number: String
var landmark: String
var area: String
var pincode: String
var place: String
}

所有结构都应该绑定(bind)相关的值/数据

最佳答案

我认为你的结构应该如下:

// 1
struct RootClass : Codable {

let data : [RunnerData]?
let error : Bool?
let status : String?
}

// 2
struct RunnerData : Codable {

let date : String?
let orderId : String?
let orderUniqueId : String?
let otpStore : String?
let product : [String]?
let qty : [String]?
let status : String?
let storeName : String?
let time : String?
let userDetails : UserDetail?

enum CodingKeys: String, CodingKey {
case date
case orderId = "order_id"
case orderUniqueId = "order_unique_id"
case otpStore = "otp_store"
case product
case qty
case status
case storeName = "store_name"
case time
case userDetails = "user_details"
}
}

// 3
struct UserDetail : Codable {

let area : String?
let landmark : String?
let name : String?
let number : String?
let pincode : String?
let place : String?
}

希望对您有所帮助。如果您还有任何疑问,请告诉我。

如果您比以前手动创建 JSON 结构,则您应该具备制作结构的适当知识。否则它会影响 JSONDecoding。有许多网站可用于在线制作您的 json 可编码类,其中之一是:http://www.jsoncafe.com/

使用此网站创建您的快速编码类。

关于ios - 如何基于json构造struct,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53883286/

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