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

时间:2018-05-24 06:29:09

标签: json swift codable

当"地址"数组不为空。但是当#34;地址"数组是空的。任何帮助将受到高度赞赏。我有Address对象的struct。基本上"地址"是#&34;类型的对象数组;地址"但是当地址为空时它会失败。

{
    "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数据时处理空数组。

2 个答案:

答案 0 :(得分:1)

我遇到了这个问题,并按照以下步骤解决了这个问题。

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

您可以尝试一下。

答案 1 :(得分:0)

真正的问题是Details中的IntString而不是Codable。解码空数组应该可以。

您的代码太复杂。享受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 } 的魔力,并省略所有CodingKeys和初始化程序,并将所有struct成员声明为非可选和常量

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

您甚至可以将成员命名为 camelCased

convertFromSnakeCase

并通过decoder.keyDecodingStrategy = .convertFromSnakeCase 密钥解码策略

{{1}}

仍然没有多余的代码!