如何在 swift 4.1 和 xcode 9.3 中使用 JSON 解码器解码嵌套的 JSON 数组和对象?
How to decode nested JSON array and object with JSONDecoder in swift 4.1 and xcode 9.3?
我是 iOS 的新手,我无法解码嵌套的 JSON 文件许多嵌套的数组和对象。我正在使用 JSONDecoder 进行解码,但我在某处出错或解码不正确。
我的解码器是
let myOrder = try JSONDecoder().decode(MyOrder.self, from: data!)
for items in myOrder.OrderDetials {
self.orders.append(MyOrders(order_id: items.order_id!, order_ref_number: items.order_ref_number!, cust_mob: items.cust_mob!, store_name: items.store_name!, store_id: items.store_id!, order_details: items.order_details!, order_amount: items.order_amount!, order_status: items.order_status!, order_status_remark: items.order_status_remark!, order_place_at: items.order_place_at!, order_status_change_at: items.order_status_change_at!))
print(items.cust_mob!)
}
print(myOrder)
它抛出的错误是
typeMismatch(Swift.Array<Any>, Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "OrderDetials", intValue: nil), _JSONKey(stringValue: "Index 0", intValue: 0), CodingKeys(stringValue: "order_details", intValue: nil)], debugDescription: "Expected to decode Array<Any> but found a string/data instead.", underlyingError: nil))
我的结构是
struct MyOrder: Decodable {
var OrderDetials: [OrderDetails]
}
struct OrderDetails: Decodable {
var order_id: String?
var order_ref_number: String?
var cust_mob: String?
var store_name: String?
var store_id: String?
var order_details: OrderItems?
var order_amount: String?
var order_status: String?
var order_status_remark: String?
var order_place_at: String?
var order_status_change_at: String?
}
struct OrderItems: Decodable {
var allorder: [AllOrder]
var TaxDetials: [TaxDetails]
struct AllOrder: Decodable {
var date_time: String?
var item: String?
var name: String?
}
struct TaxDetails: Decodable {
var tax_per: String?
var tax_name: String?
}
}
和数据模型class
class MyOrders {
var order_id: String
var order_ref_number: String
var cust_mob: String
var store_name: String
var store_id: String
var order_details: OrderItems?
var order_amount: String
var order_status: String
var order_status_remark: String
var order_place_at: String
var order_status_change_at: String?
init(order_id: String, order_ref_number: String, cust_mob: String, store_name: String, store_id: String, order_details: OrderItems?, order_amount: String, order_status: String, order_status_remark: String, order_place_at: String, order_status_change_at: String?) {
self.order_id = order_id
self.order_ref_number = order_ref_number
self.cust_mob = cust_mob
self.store_name = store_name
self.store_id = store_id
self.order_details = order_details
self.order_amount = order_amount
self.order_status = order_status
self.order_status_remark = order_status_remark
self.order_place_at = order_place_at
self.order_status_change_at = order_status_change_at
}
}
class MyItems {
var item: String
var name: String
var price: String
init(item: String, name: String, price: String) {
self.item = item
self.name = name
self.price = price
}
}
我的JSON是
{
"OrderDetials": [
{
"order_id": "11",
"order_ref_number": "MM1525235813",
"cust_mob": "9958324531",
"store_name": "Moti mahal",
"store_id": "1",
"order_details": "{'allorder':[{\"date_time\":\"\",\"item\":\"2\",\"name\":\"CHICKN ROLL\",\"price\":\"360\"}]}{\"TaxDetials\":[{\"tax_per\":\"5\",\"tax_name\":\"CGST & SGST\"}]}",
"order_amount": "378.0",
"order_status": "1",
"order_status_remark": "New_Order_Place",
"order_place_at": "2018-05-02 10:06:53",
"order_status_change_at": null
}
]
}
请有人告诉我如何正确解码或我需要在结构上做哪些更改。
OrderDetials
是 Array of Dictionary
[[String: String?]]
,您的 Initial
响应也是字典 order_details
是 Wrong json String
而不是 Dictionary
,所以我们将把它解析为字符串,在 Codable
中,每个字典 {}
都被转换为 Struct
或 Class
并且 []
是 Array
型号:
import Foundation
struct Initial: Codable {
let orderDetials: [OrderDetial]
enum CodingKeys: String, CodingKey {
case orderDetials = "OrderDetials"
}
}
struct OrderDetial: Codable {
let orderID, orderRefNumber, custMob, storeName: String
let storeID, orderDetails, orderAmount, orderStatus: String
let orderStatusRemark, orderPlaceAt: String
let orderStatusShangeAt:String?
enum CodingKeys: String, CodingKey {
case orderID = "order_id"
case orderRefNumber = "order_ref_number"
case custMob = "cust_mob"
case storeName = "store_name"
case storeID = "store_id"
case orderDetails = "order_details"
case orderAmount = "order_amount"
case orderStatus = "order_status"
case orderStatusRemark = "order_status_remark"
case orderPlaceAt = "order_place_at"
case orderStatusShangeAt = "order_status_change_at"
}
}
// MARK: Convenience initializers
extension Initial {
init(data: Data) throws {
self = try JSONDecoder().decode(Initial.self, from: data)
}
}
像这样使用:
if let initial = try? Initial.init(data: data){
for item in initial.orderDetials {
print(item.orderAmount)
print(item.orderDetails) // here is Wrong format from your Server API
}
}
Order_details是错误的
{'allorder':[{"date_time":"","item":"2","name":"CHICKN ROLL","price":"360"}]}{"TaxDetials":[{"tax_per":"5","tax_name":"CGST & SGST"}]}
我是 iOS 的新手,我无法解码嵌套的 JSON 文件许多嵌套的数组和对象。我正在使用 JSONDecoder 进行解码,但我在某处出错或解码不正确。
我的解码器是
let myOrder = try JSONDecoder().decode(MyOrder.self, from: data!)
for items in myOrder.OrderDetials {
self.orders.append(MyOrders(order_id: items.order_id!, order_ref_number: items.order_ref_number!, cust_mob: items.cust_mob!, store_name: items.store_name!, store_id: items.store_id!, order_details: items.order_details!, order_amount: items.order_amount!, order_status: items.order_status!, order_status_remark: items.order_status_remark!, order_place_at: items.order_place_at!, order_status_change_at: items.order_status_change_at!))
print(items.cust_mob!)
}
print(myOrder)
它抛出的错误是
typeMismatch(Swift.Array<Any>, Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "OrderDetials", intValue: nil), _JSONKey(stringValue: "Index 0", intValue: 0), CodingKeys(stringValue: "order_details", intValue: nil)], debugDescription: "Expected to decode Array<Any> but found a string/data instead.", underlyingError: nil))
我的结构是
struct MyOrder: Decodable {
var OrderDetials: [OrderDetails]
}
struct OrderDetails: Decodable {
var order_id: String?
var order_ref_number: String?
var cust_mob: String?
var store_name: String?
var store_id: String?
var order_details: OrderItems?
var order_amount: String?
var order_status: String?
var order_status_remark: String?
var order_place_at: String?
var order_status_change_at: String?
}
struct OrderItems: Decodable {
var allorder: [AllOrder]
var TaxDetials: [TaxDetails]
struct AllOrder: Decodable {
var date_time: String?
var item: String?
var name: String?
}
struct TaxDetails: Decodable {
var tax_per: String?
var tax_name: String?
}
}
和数据模型class
class MyOrders {
var order_id: String
var order_ref_number: String
var cust_mob: String
var store_name: String
var store_id: String
var order_details: OrderItems?
var order_amount: String
var order_status: String
var order_status_remark: String
var order_place_at: String
var order_status_change_at: String?
init(order_id: String, order_ref_number: String, cust_mob: String, store_name: String, store_id: String, order_details: OrderItems?, order_amount: String, order_status: String, order_status_remark: String, order_place_at: String, order_status_change_at: String?) {
self.order_id = order_id
self.order_ref_number = order_ref_number
self.cust_mob = cust_mob
self.store_name = store_name
self.store_id = store_id
self.order_details = order_details
self.order_amount = order_amount
self.order_status = order_status
self.order_status_remark = order_status_remark
self.order_place_at = order_place_at
self.order_status_change_at = order_status_change_at
}
}
class MyItems {
var item: String
var name: String
var price: String
init(item: String, name: String, price: String) {
self.item = item
self.name = name
self.price = price
}
}
我的JSON是
{
"OrderDetials": [
{
"order_id": "11",
"order_ref_number": "MM1525235813",
"cust_mob": "9958324531",
"store_name": "Moti mahal",
"store_id": "1",
"order_details": "{'allorder':[{\"date_time\":\"\",\"item\":\"2\",\"name\":\"CHICKN ROLL\",\"price\":\"360\"}]}{\"TaxDetials\":[{\"tax_per\":\"5\",\"tax_name\":\"CGST & SGST\"}]}",
"order_amount": "378.0",
"order_status": "1",
"order_status_remark": "New_Order_Place",
"order_place_at": "2018-05-02 10:06:53",
"order_status_change_at": null
}
]
}
请有人告诉我如何正确解码或我需要在结构上做哪些更改。
OrderDetials
是 Array of Dictionary
[[String: String?]]
,您的 Initial
响应也是字典 order_details
是 Wrong json String
而不是 Dictionary
,所以我们将把它解析为字符串,在 Codable
中,每个字典 {}
都被转换为 Struct
或 Class
并且 []
是 Array
型号:
import Foundation
struct Initial: Codable {
let orderDetials: [OrderDetial]
enum CodingKeys: String, CodingKey {
case orderDetials = "OrderDetials"
}
}
struct OrderDetial: Codable {
let orderID, orderRefNumber, custMob, storeName: String
let storeID, orderDetails, orderAmount, orderStatus: String
let orderStatusRemark, orderPlaceAt: String
let orderStatusShangeAt:String?
enum CodingKeys: String, CodingKey {
case orderID = "order_id"
case orderRefNumber = "order_ref_number"
case custMob = "cust_mob"
case storeName = "store_name"
case storeID = "store_id"
case orderDetails = "order_details"
case orderAmount = "order_amount"
case orderStatus = "order_status"
case orderStatusRemark = "order_status_remark"
case orderPlaceAt = "order_place_at"
case orderStatusShangeAt = "order_status_change_at"
}
}
// MARK: Convenience initializers
extension Initial {
init(data: Data) throws {
self = try JSONDecoder().decode(Initial.self, from: data)
}
}
像这样使用:
if let initial = try? Initial.init(data: data){
for item in initial.orderDetials {
print(item.orderAmount)
print(item.orderDetails) // here is Wrong format from your Server API
}
}
Order_details是错误的
{'allorder':[{"date_time":"","item":"2","name":"CHICKN ROLL","price":"360"}]}{"TaxDetials":[{"tax_per":"5","tax_name":"CGST & SGST"}]}