从api下载响应时出现错误

时间:2018-11-01 05:50:11

标签: swift nsjsonserialization jsondecoder

我正在使用模型类从api下载json,但无法下载,请允许我显示我的代码和预期的响应

回复

SUCCESS: {
    "operator_list" =     (
                {
            "A_ADDRESS" = "1001, CASABLANCA, SKYLINE OASIS COMPLEX, PREMIER ROAD, VIDHYAVIHA";
            "A_BLOOD_GROUP" = 1;
            "A_CURRENT_ACTIVITY" = 1;
            "A_DOB" = "{\"D\":\"24\",\"M\":\"October\",\"Y\":\"1954\"}";
            "A_EMAIL" = "VELANIMANSUKH@GMAIL.COM";
            "A_FAMILY_ID" = 000001;
            "A_FATHER_NAME" = DHANJI;
            "A_FULLNAME" = MANSUKH;
            "A_MERITAL_STATUS" = 2;
            "A_N_city" = Madanpura;
            "A_PHONE" = 9892135134;
            "A_SURNAME" = 2;
            age = "";
            city = Mumbai;
            education = "<null>";
            state = Maharashtra;
            "sur_name" = VELANI;
        }
);
}

现在我将向您展示我的模型课

import UIKit

class OperatorList: Codable{
    let operator_list : [operator_list]
    init(operator_list: [operator_list]){
        self.operator_list = operator_list
     }
}


class operator_list: Codable {
    let A_ADDRESS : String
    let A_BLOOD_GROUP : String
    let A_CURRENT_ACTIVITY : String
    let A_DOB : String
    let A_EMAIL : String
    let A_FAMILY_ID : String
    let A_FATHER_NAME : String
    let A_FULLNAME  : String
    let A_MERITAL_STATUS : String
    let A_N_city : String
    let A_PHONE : String
    let A_SURNAME : String
    let age : String
    let city : String
    let education : String
    let state : String
    let sur_name : String

    init(A_ADDRESS: String,A_BLOOD_GROUP:String,A_CURRENT_ACTIVITY: String,A_DOB:String,A_EMAIL:String,A_FAMILY_ID:String,A_FATHER_NAME:String,A_FULLNAME:String,A_MERITAL_STATUS:String,A_N_city:String,A_PHONE: String,A_SURNAME:String,age:String,city:String,education:String,state:String,sur_name:String){
        self.A_ADDRESS = A_ADDRESS
         self.A_BLOOD_GROUP = A_BLOOD_GROUP
         self.A_CURRENT_ACTIVITY = A_CURRENT_ACTIVITY
         self.A_DOB = A_DOB
         self.A_EMAIL = A_EMAIL
         self.A_FAMILY_ID = A_FAMILY_ID
         self.A_FATHER_NAME = A_FATHER_NAME
         self.A_FULLNAME = A_FULLNAME
         self.A_MERITAL_STATUS = A_MERITAL_STATUS
         self.A_N_city = A_N_city
         self.A_PHONE = A_PHONE
         self.A_SURNAME = A_SURNAME
         self.age = age
         self.city = city
         self.education = education
         self.state = state
         self.sur_name = sur_name
    }

}

这是我的模型类,请告诉我模型类检查中是否有错误

现在我将向您展示api调用方法

    func calSearchAPI(){
        guard let dURl = url else{ return }
        URLSession.shared.dataTask(with: dURl) { (data, urlResponse, error) in
            print("Downloaded")
            guard let data = data, error == nil, urlResponse != nil else{
                print("Something Went Wrong")
                return
            }
             print("Downloaded")
            do{
                let decoder = JSONDecoder()
                let operator_list = try decoder.decode(OperatorList.self, from: data)
                print(operator_list)
            }catch{
                print("Something Went Wrong After Download")
            }
        }.resume()
    }

请告诉我确切的问题在哪里

1 个答案:

答案 0 :(得分:0)

我已经创建了相同的场景,只是以正确的格式重写了A_DOB

let jsonStr = """
{
"operator_list": [
    {
        "A_FAMILY_ID": "000001",
        "A_FULLNAME": "MANSUKH",
        "A_FATHER_NAME": "DHANJI",
        "A_SURNAME": "2",
        "A_DOB":
        {
            "D":"24",
            "M":"October",
            "Y":"1954"
        },
        "sur_name": "VELANI",
        "A_PHONE": "9892135134",
        "A_CURRENT_ACTIVITY": "1",
        "A_MERITAL_STATUS": "2",
        "A_ADDRESS": "1001, CASABLANCA, SKYLINE OASIS COMPLEX, PREMIER ROAD, VIDHYAVIHA",
        "A_EMAIL": "VELANIMANSUKH@GMAIL.COM",
        "A_BLOOD_GROUP": "1",
        "city": "Mumbai",
        "state": "Maharashtra",
        "A_N_city": "Madanpura",
        "education": "null",
        "age": ""
    }
]
}
"""

class OperatorList: Codable{
    let operator_list : [operator_list]
}

class DOB: Codable {
    let D: String
    let M: String
    let Y: String
}

class operator_list: Codable {
    let A_ADDRESS : String
    let A_BLOOD_GROUP : String
    let A_

CURRENT_ACTIVITY : String
    let A_DOB : DOB
    let A_EMAIL : String
    let A_FAMILY_ID : String
    let A_FATHER_NAME : String
    let A_FULLNAME  : String
    let A_MERITAL_STATUS : String
    let A_N_city : String
    let A_PHONE : String
    let A_SURNAME : String
    let age : String
    let city : String
    let education : String
    let state : String
    let sur_name : String
}

if let data = jsonStr.data(using: .utf8) {
    do {
        let response = try JSONDecoder().decode(OperatorList.self, from: data)
        print(response.operator_list)
    } catch  {
        print(error)
    }
}