如何在可编码解码过程中处理空数组



当"address"数组不为空时,它工作得很好。但是当"地址"数组为空时,它会失败。任何帮助将不胜感激。我有地址对象的结构。基本上"地址"是"地址"类型的对象的数组,但是当地址为空时,它会失败。

{
"success": "1",
"message": "You have succesfully verified your mobile no",
"details": {
"customer_id": 825,
"is_delivery_available": "0",
"is_registration_complete": "0",
"is_customer_verified": "0",
"customer_status": "0",
"cart_count": "0",
"name_type": "mr",
"firstname": "",
"lastname": "",
"full_name": "",
"pincode": "",
"profile_pic": "",
"mobile": "8583846677",
"email": "",
"address": [
],
"referral_code": ""
}
}

以上是我尝试在 Swift4 中使用 Codable 解码的 JSON。

import Foundation
struct Signup: Codable {
var success:String?
var message:String?
var details:Details?
private enum CodingKeys: String, CodingKey { case success, message, details }
init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
success = try values.decode(String.self, forKey: .success)
message = try values.decode(String.self, forKey: .message)
if let _details = try? values.decode(Details.self, forKey: .details) {
details = _details
}
}
}
struct Details: Codable {
var address:[Address]?
var cart_count:String?
var customer_id:String?
var customer_status:String?
var email:String?
var firstname:String?
var full_name:String?
var is_customer_verified:String?
var is_delivery_available:String?
var is_registration_complete:String?
var lastname:String?
var mobile:String?
var name_type:String?
var pincode:String?
var profile_pic:String?
var referral_code:String?
private enum CodingKeys: String, CodingKey { case address, cart_count, customer_id, customer_status, email, firstname, full_name, is_customer_verified, is_delivery_available, is_registration_complete, lastname, mobile, name_type, pincode, profile_pic, referral_code }
init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
****-->>> Issue is in this line. How to handle empty array while decoding. <<<--****
address = try? values.decode([Address].self, forKey: .address) 
cart_count = try values.decode(String.self, forKey: .cart_count)
customer_id = try values.decode(String.self, forKey: .customer_id)
customer_status = try values.decode(String.self, forKey: .customer_status)
email = try values.decode(String.self, forKey: .email)
firstname = try values.decode(String.self, forKey: .firstname)
full_name = try values.decode(String.self, forKey: .full_name)
is_customer_verified = try values.decode(String.self, forKey: .is_customer_verified)
is_delivery_available = try values.decode(String.self, forKey: .is_delivery_available)
is_registration_complete = try values.decode(String.self, forKey: .is_registration_complete)
lastname = try values.decode(String.self, forKey: .lastname)
mobile = try values.decode(String.self, forKey: .mobile)
name_type = try values.decode(String.self, forKey: .name_type)
pincode = try values.decode(String.self, forKey: .pincode)
profile_pic = try values.decode(String.self, forKey: .profile_pic)
referral_code = try values.decode(String.self, forKey: .referral_code)
}
}
struct Address: Codable {
var address_default:String?
var address_id:String?
var address_type:String?
var city:String?
var customer_id:String?
var flat_or_house_or_office_no:String?
var full_address:String?
var full_name:String?
var landmark:String?
var lat:String?
var lng:String?
var mobile:String?
var name_type:String?
var pincode:String?
var street_or_society_or_office_name:String?
}

我被困在这个里面。如何在 Swift4 Codable 中解码 json 数据时处理空数组。

我遇到了这个问题,我做了如下来解决这个问题。

if let _address = try? values.decode([Address].self, forKey: .address) {
address = _address
} else {
address = []
}

你可以试一试。


2022.06.08 更新。

根据@Amin贝纳里布

let _address = try? values.decode([Address].self, forKey: .address) ?? []

真正的问题是Details中的customer_idInt不是String。解码一个空数组应该可以工作。

你的代码太复杂了。享受Codable的魔力,省略所有 CodingKeys 和初始值设定项,并将所有结构成员声明为非可选和常量

struct Signup: Decodable {
let success : String
let message : String
let details : Details
}
struct Details : Decodable {
let address : [Address]
let cart_count : String
let customer_id : Int
let customer_status, email, firstname, full_name : String
let is_customer_verified, is_delivery_available, is_registration_complete : String
let lastname, mobile, name_type, pincode, profile_pic, referral_code : String
}
struct Address: Decodable {
let address_default, address_id, address_type, city, customer_id : String
let flat_or_house_or_office_no, full_address, full_name : String
let landmark, lat, lng, mobile, name_type, pincode, street_or_society_or_office_name : String
}

您甚至可以将成员命名为骆驼

let cartCount : String
let customerId : Int
...
let isRegistrationComplete : String

并通过convertFromSnakeCase密钥解码策略

decoder.keyDecodingStrategy = .convertFromSnakeCase

仍然没有额外的代码!

相关内容

  • 没有找到相关文章

最新更新