JSONDecoder实现转Model:

今天和后台联调项目赠卡发卡功能的时候一直提示报错,然而后台一直说数据请求数据返回的是成功200,并且数据都是对的,导致的结果是查记录发卡是成功的,只是提示数据格式报错。。。。。。

一直找不出问题,因为这块请求不管是接口,还是调用的方法和之前的储值卡发卡的代码是一模一样的,没有任何区别,最后找了好久发现是赠卡新加了一个实体,我赶紧写一个代码验证一下,发现还真是数据类型转换的问题,这里记录下:

JOSN文件:

。。。
"cardFaceValue":50
。。。

Model文件:

//  HDLNFCCardCustomerInfoModel.swift
//  HDLNFCCardRequest
//
//  Created by 海底捞lzx on 2017/11/16.
//  Copyright © 2017年 海底捞. All rights reserved.
//

import Foundation

/// 公共请求信息
public struct HDLNFCRequestCommonInfo {
    /// token
    public var token: String = ""
    /// 服务员名称
    public var waiterName: String = ""
}
public var nfcCommonInfo: HDLNFCRequestCommonInfo = HDLNFCRequestCommonInfo()

/// 请求返回格式
public struct HDLNFCModelResponse: Codable {
    public struct HDLNFCModelCardInfo: Codable {
        
        public struct HDLNFCCardInfo: Codable {
            /// 卡信息
            public struct HDLNFCCardType: Codable {
                ///  例:"2"
                public var accEnableType: String?
                /// 能否充值 例:"Y"
                public var canRechargable: String?
                /// 失效天数 例:0
                public var effectDays: Int?
                /// 失效日期 例:2457820800000
                public var effectdate: Date?
                /// 是否生效 例:"Y"
                public var hasEffect: String?
                /// 最大充值金额 例:5000
                public var maximum: Int?
                /// 卡种编码 例:"3"
                public var typeCat: String?
                /// 卡种名称 例:"赠卡"
                public var typeName: String?
                
                public init() {
                }
            }
            /// 卡片状态
            public enum HDLNFCCardStatus: String, Codable {
                case new        = "CRM_CUSTCARDS_STATE_NEW"
                case lost       = "CRM_CUSTCARDS_STATE_LOST"
                case normal     = "CRM_CUSTCARDS_STATE_NORMAL"
                case disable    = "CRM_CUSTCARDS_STATE_DISABLE"
                case quitdel    = "CRM_CUSTCARDS_STATE_QUITDEL"
            }
            
            /// 卡种
            public enum HDLNFCCardTypeNo: String, Codable {
                /// 充值卡
                case recharge   = "001"
                /// 赠卡
                case present    = "002"
            }
            
            /// 本期消费次数 例:"0.0"
            public var bcxfcs: String?
            /// 本期充值赠送账户 例:"500"
            public var bqczzszh: String?
            /// 本期充值主账户 例:"0.0"
            public var bqczzzh: String?
            /// 本期消费 例:"443.0"
            public var bqxf: String?
            /// 本期消费赠送账户 例:"443.0"
            public var bqxfzszh: String?
            /// 本期消费主账户 例:"0.0"
            public var bqxfzzh: String?
            /// 本次市别 例:晚市
            public var bbbc: String?
            /// 本次账单日期 例:2017-11-24
            public var bbrq: String?
            /// 本次积分 例:"0"
            public var bcjf: String?
            /// 本次快卡金额 例:"0"
            public var bckkpje: String?
            /// 本机金额 例:"0"
            public var bjje: String?
            /// 备注 例:"测试打印"
            public var bz: String?
            
            /// 卡号 例:"ZS00000144022"
            public var cardNo: String?
            /// 卡片类别信息
            public var cardtype: HDLNFCCardType?
            /// 充值次数  1
            public var czcs: String?
            /// 充值金额 例:"0"
            public var czje: String?
            
            // 充值方式
            public var paymentType: String?
            /// 充值类型 例:"20"
            public var czlx: String?
            /// 充值时间 例:"2017-08-06 14:28:16.0"
            public var czsj: String?
            /// 充值SL 例:"2"
            public var czsl: String?
            /// czym 例:"10000565",
            public var czym: String?
            /// "2017111400010"
            public var csxzfs: String?
            /// 制卡时间 例:
            public var createCardTime: String?
            
            /// 当前赠送账户余额 例:"57.0"
            public var dqzszhye: String?
            /// 当前主账户余额 例:"0.0"
            public var dqzzhye: String?
            /// "0"
            public var dqcs: String?
            
            /// 发卡机构编号 例:"020114"
            public var fkjgbh: String?
            /// 发卡门店名称 例:"北京十四店"
            public var fkjgmc: String?
            /// 发卡机构序号 例:"38"
            public var fkjgxh: String?
            /// 发卡时间 例:"2017-07-18 10:23:52.0"
            public var fksj: String?
            /// 发卡账户类型 例:"CRM_FKZHLX_HOST"
            public var fkzhlx: String?
            /// fkzhlex 例:"CRM_FHZHLX_HOST",
            public var fkzhlex: String?
            /// ""
            public var fkhylx: String?
            /// ""
            public var fkfjzkje: String?
            
            /// 会员编号 例:"ZS00000144022"
            public var hybh: String?
            /// 会员卡号 例:"ZS00000144022"
            public var hykh: String?
            /// 客户名称 例:"崔先生"
            public var hymc: String?
            
            /// 开卡金额 例:"500.0"
            public var kkpje: String?
            /// 卡类型 例: ""
            public var klx: String?
            /// "YK"
            public var kjsx: String?
            /// "0.0",
            public var kncs: String?
            /// "57.0",
            public var knje: String?
            /// "57.0",
            public var knye: String?
            /// "CRM_CUSTCARDS_STATE_NORMAL",
            public var kpzt: HDLNFCCardStatus?
            /// "57.0",
            public var kyed: String?
            /// "002",
            public var kzbh: HDLNFCCardTypeNo?
            /// "0"
            public var kxfts: String?
            
            /// "500.0",
            public var ljczzszh: String?
            /// "0.0",
            public var ljczzzh: String?
            /// "443.0",
            public var ljxf: String?
            /// "443.0",
            public var ljxfzszh: String?
            /// "0.0",
            public var ljxfzzh: String?
            /// "0"
            public var ljjf: String?
            
            /// "",
            public var password: String?
            
            /// "2017111400010"
            public var qtzdbh: String?
            
            /// "N"
            public var sfys: String?
            /// "C13352966C000000A60000000016E000"
            public var shopId: String?
            /// "0",
            public var skje: String?
            
            // 退卡原因
            public var tkyy: String?
            
            // 退卡金额
            public var tkje: String?
            
            /// 0
            public var skpje: Double?
            /// 手机号码 例:
            public var sjhm: String?
            
            /// "1"
            public var tableno: String?
            /// ""
            public var tzlx: String?
            
            ///"0.0",
            public var xbqjf: String?
            ///"0.0",
            public var xljjf: String?
            /// "1"
            public var xfje: String?
            /// "2017-11-24 16:19:56"
            public var xfsj: String?
            /// "0"
            public var xfks: String?
            /// 销售人员编码 例:10069454
            public var xsry: String?
            /// 0
            public var xzcs: Double?
            /// 0
            public var xzed: Double?
            
            /// "0.0",
            public var ykpje: String?
            /// "2047-11-20 00:00:00.0",
            public var yxrq: String?
            /// "0",
            public var yxts: String?
            /// "2047-11-20 00:00:00.0"
            public var yyxrq: String?
            /// "0"
            public var yljjf: String?
            /// 0
            public var ybqjf: String?
            /// ""
            public var yhykh: String?
            /// "0"
            public var ysje: String?
            ///  "02",
            public var yydbh: String?
            /// "0.0"
            public var yzszhye: String?
            /// "1032.32"
            public var yzzhye: String?
            
            /// "10001037"
            public var zdbh: String?
            /// "1"
            public var zdje: String?
            /// 证件号码 例:wu
            public var zjhm: String?
            /// "0"
            public var zscs: String?
            /// "0"
            public var zsje: String?
             /// "0"
            public var zszhxfje: String?
            /// "1"
            public var zzhxfje: String?
            
            public init() {
            }
            public init(cardNo: String) {
                self.cardNo = cardNo
                self.hykh = cardNo
            }
        }
        
        /// 天子星信息
        public struct HDLNFCTZXInfo: Codable {
            ///"TZX-StoreBusinessSystem",
            public var app: String?
            ///"20051024092733000440",
            public var corid: String?
            ///"53",
            public var des: String?
            ///"00",
            public var devid: String?
            ///"{D4AA2569-5A88-4A34-B476-CCD7E9FE7A07}",
            public var msgid: String?
            ///"888",
            public var personnel: String?
            ///"STRING",
            public var reserve: String?
            ///"15",
            public var src: String?
            ///"1.0",
            public var tid: String?
            ///"TXDTASHUOCHW35DG39SG0LHHAW04YSDFGH",
            public var version: String?
            ///"2017-11-21"
            public var workdate: String?
            /// 证件号码 例:wu
            public var zjhm: String?
            
            public init() {
            }
        }
        
        /// 顾客信息
        public struct HDLNFCCustomerInfo: Codable {
            ///"1900-01-01 00:00:00.0",字段说明:出生日期
            public var csrq: String?
            ///"崔先生",字段说明:客户名称
            public var hymz: String?
            ///"CRM_SEX_MAN",字段说明:性别
            public var hyxb: String?
            ///"CRM_NO",
            public var sfgs: String?
            ///"15811454461",字段说明:手机号
            public var sjhm: String?
            ///"10069454",字段说明:销售人员编码
            public var xsry: String?
            ///"CRM_YSE",
            public var zfbj: String?
            ///"CRM_CUSTOMERBASE_PAPER_IDCARD"
            public var zjlx: String?
            /// yyah 例:"适中;"
            public var yyah: String?
            /// 证件号码 例:wu
            public var zjhm: String?
            
            public init() {
            }
        }
        
        /// 支付信息
        public struct HDLNFCPayment: Codable {
            /// 发卡金额 例: "0"
            public var fkje: String?
            /// 发卡类型编号 例:"ERP_PAYMENTS_CREDIT"
            public var fklxbh: String?
            /// "K02",
            public var htsksx: String?
            /// "01"
            public var sklx: String?
            
            public init() {
            }
        }
        
        // 赠卡信息
        public struct HDLNFCPresentCardInfo: Codable {
            public var id: String?
            public var companyId: String?
            public var shopId: String?
            public var shopName: String?
            public var cardNo: String?  //卡号
            public var cardtype: String? //卡类型
            public var cardUse: String?//卡用途
            public var cardFaceValue: String?//卡面值
            public var creatorId: String?//制卡工号
            public var creatorName: String?//制卡名字
            public var makePhone: String?//制卡人员电话
            public var creatime: String?//制卡时间
            public var status: String?//状态0未赠送1已赠送
            public var donorId: String?//赠送工号
            public var donorName: String?//赠送名字
            public var doneeName: String?//受赠名字
            public var doneePhone: String?//受赠电话
            public var tableNo: String?//桌台号
            public var presentCreateTime: String?//受赠时间
            public var presentReason: String?//受赠理由
            public var presentIdCard: String?//身份证号
            public var serial: String?;
            
            public init() {
            }
        }
        
        public var cards: [HDLNFCCardInfo]?
        public var head: HDLNFCTZXInfo?
        public var members: [HDLNFCCustomerInfo]?
        public var payments: [HDLNFCPayment]?
        public var presentCardInfo: HDLNFCPresentCardInfo?
        public var retCusInfos: [[String : String]]?
        /// "2e966f2bc5c7b2c6324cc03bd0ef2cfe"
        public var sig: String?
        public var serial: Int?
        public var shopNo: String?
        public init() {
        }
        
//        enum CodingKeys: String, CodingKey {
////            case cards = "obj[cards]"
////            case head = "obj[head]"
////            case members = "obj[members]"
//            case obj
//        }
//
//        init(from decoder: Decoder) throws {
//
//        }
    }
    public var message: String?
    public var obj: HDLNFCModelCardInfo?
    public var result: Int
    public init(message: String?, obj: HDLNFCModelCardInfo?, result: Int) {
        self.message = message
        self.obj = obj
        self.result = result
    }
   
}

验证的程序代码:


    override func viewDidLoad() {
        super.viewDidLoad()
        let path = Bundle.main.path(forResource: "Present", ofType: "json")
        let url = URL(fileURLWithPath: path!)
        // 带throws的方法需要抛异常
        do {
            /*
             * try 和 try! 的区别
             * try 发生异常会跳到catch代码中
             * try! 发生异常程序会直接crash
             */
            let data = try Data(contentsOf: url)
            let jsonData:Any = try JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.mutableContainers)
            print("present = ", jsonData)
            let decoder = JSONDecoder()
            let model = try decoder.decode(HDLNFCModelResponse.self, from: data)
            if let modelResponse = model as? HDLNFCModelResponse {
                print("\nmessage:\(modelResponse.message ?? "no Message")")
                print("code:\(modelResponse.result)")
                print(modelResponse)
            }
        } catch let error as Error? {
            print("读取本地数据出现错误!",error ?? "")
        }
    }
}

结果当然是解析失败,原因是public var cardFaceValue: String?
最后改成Double类型就可以正常解析了。

还是疑问难道JSONDecoder解析转Model和数据类型有关么?再说其他基本类型转String应该没问题吧。。。。。。

知道的给我留言告诉我哟~~~

你可能感兴趣的:(JSONDecoder实现转Model:)