-2
code = 200;
msg = "Verification_OTP";
result =     {
    "customer_email" = "ghg@Gmail.com";
    "customer_name" = we;
    "customer_phone" = 1234567890;
    otp = 658715;
    "user_id" = 135;
};

I am unable to parse i got nil in response.Here is my code

struct Root: Codable {
let code: Int?
let msg: String?
let customerModel: Result?
}
struct Result: Codable {
    let customerName:String?
    let customerEmail:String?
    let customerMobile:String?
    let otp:Int?
    let userId:Int?
    enum CodingKeys: String ,CodingKey {
        case customerName = "customer_name"
        case customerEmail = "customer_email"
        case customerMobile = "customer_no"
        case userId = "user_id"
        case otp = "otp"
    }

}
Nordle
  • 2,915
  • 3
  • 16
  • 34
Akshay
  • 425
  • 1
  • 5
  • 16
  • 2
    Add a valid JSON here. – PGDev Jun 11 '19 at 12:03
  • @PGDev That's just how is printed a NSDictionary. Nothing fancy about that. – Larme Jun 11 '19 at 13:09
  • check your keys, for example `"customer_no"` in the keys should be `"customer_phone"` – Daniel Jun 11 '19 at 13:10
  • And where do you decode? Where do you read the error? There should be one about `result`/`customerModel` because there is no `customerModel` at top level of you JSON, but a key `result`. You need to do a `enum CodingKeys` to tell that the value of `result` needs to be parsed into `customerModel`. – Larme Jun 11 '19 at 13:11

2 Answers2

2

1. If your JSON response is like:

{
    "code": 200,
    "msg": "Verification_OTP",
    "result": {
        "customer_email": "ghg@Gmail.com",
        "customer_name": "we",
        "customer_phone": 1234567890,
        "otp": 658715,
        "user_id": 135
    }
}

2. Your Codable models will be like:

struct Response: Codable {
    let code: Int
    let msg: String
    let result: Result
}

struct Result: Codable {
    let customerEmail: String
    let customerName: String
    let customerPhone: Int
    let otp: Int
    let userId: Int
}

3. Parse the data with above models like:

do {
    let decoder = JSONDecoder()
    decoder.keyDecodingStrategy = .convertFromSnakeCase
    let response = try decoder.decode(Response.self, from: data)
    print(response)
} catch {
    print(error)
}
PGDev
  • 23,751
  • 6
  • 34
  • 88
  • The data couldn’t be read because it isn’t in the correct format. – Akshay Jun 11 '19 at 14:43
  • Add the JSON format you’re getting. Then only I’ll be able to help you further. For the JSON format I’ve used, The code is working perfectly fine. See if there’re any changes in the Data Types I’ve used and are there any null values in the JSON. – PGDev Jun 11 '19 at 14:51
  • SUCCESS: { code = 200; msg = "Verification_OTP"; result = { "customer_email" = "sdfds@gmail.con"; "customer_name" = "sdf day"; "customer_phone" = 1234567890; otp = 915689; "user_id" = 201; }; } – Akshay Jun 11 '19 at 16:31
  • i am able to decode whole json except when i am adding user_id . user_id is not working as i am taking it Int or String both but its not working.. – Akshay Jun 11 '19 at 16:34
  • keyNotFound(CodingKeys(stringValue: "user_id", intValue: nil), Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "result", intValue: nil)], debugDescription: "No value associated with key CodingKeys(stringValue: \"user_id\", intValue: nil) (\"user_id\").", underlyingError: nil)) – Akshay Jun 11 '19 at 16:48
  • What is the type of user_id that you’re getting in the response? Is if something other than Int? – PGDev Jun 11 '19 at 16:49
  • from the response it should be int . I have tried both the ways int as well as String data type also but nothing happened – Akshay Jun 12 '19 at 03:50
0

Change enum case and also check customer_phone type i think it should be Int :

case customerMobile = "customer_phone"
Jogendar Choudhary
  • 3,476
  • 1
  • 12
  • 26