简体   繁体   English

如何存储 Json 注册响应 API? 并出现“无法读取数据,因为它的格式不正确”的错误。

[英]How to Store Json Response Of Registration API ? and Getting error of "The data couldn’t be read because it isn’t in the correct format."

Hello I'm trying to store my json Response to My Model Class, But I'm getting an error "The data couldn't be read because it isn't in the correct format."您好,我正在尝试存储我的 json 对我的 Model Class 的响应,但我收到错误消息“无法读取数据,因为它的格式不正确。” My Api is working Perfectly fine, only issue is Not storing my data into Data Model Class.我的 Api 工作得非常好,唯一的问题是没有将我的数据存储到数据 Model Class 中。

Here's My Details这是我的详细信息

My Json Response我的 Json 响应

{
"flag": "1",
"message": "Data inserted.",
"customerId": "60",
"firstName": "fatehlal",
"lastName": "pra",
"animalName": "cat",
"isAround": "1",
"comapnyId": "2",
"company_logo": "http://192.168.1.228/public/img/logosign.png",
"company_name": "Admin",
"mobileNumber": "2345678901"
}

My Register Model我的寄存器 Model

import Foundation

struct RegisterModel : Codable {
let flag : String?
let message : String?
let customerId : String?
let firstName : String?
let lastName : String?
let animalName : String?
let isAround : String?
let comapnyId : String?
let company_logo : String?
let company_name : String?
let mobileNumber : String?

enum CodingKeys: String, CodingKey {

    case flag = "flag"
    case message = "message"
    case customerId = "customerId"
    case firstName = "firstName"
    case lastName = "lastName"
    case animalName = "animalName"
    case isAround = "isAround"
    case comapnyId = "comapnyId"
    case company_logo = "company_logo"
    case company_name = "company_name"
    case mobileNumber = "mobileNumber"
}

init(from decoder: Decoder) throws {
    let values = try decoder.container(keyedBy: CodingKeys.self)
    flag = try values.decodeIfPresent(String.self, forKey: .flag)
    message = try values.decodeIfPresent(String.self, forKey: .message)
    customerId = try values.decodeIfPresent(String.self, forKey: .customerId)
    firstName = try values.decodeIfPresent(String.self, forKey: .firstName)
    lastName = try values.decodeIfPresent(String.self, forKey: .lastName)
    animalName = try values.decodeIfPresent(String.self, forKey: .animalName)
    isAround = try values.decodeIfPresent(String.self, forKey: .isAround)
    comapnyId = try values.decodeIfPresent(String.self, forKey: .comapnyId)
    company_logo = try values.decodeIfPresent(String.self, forKey: .company_logo)
    company_name = try values.decodeIfPresent(String.self, forKey: .company_name)
    mobileNumber = try values.decodeIfPresent(String.self, forKey: .mobileNumber)
}

}

My Alamofire Method我的 Alamofire 方法

  var registerDetail : RegisterModel?
AF.request("http://192.168.1.228/public/api/register", method: .post, parameters: param, encoding: JSONEncoding.default, headers: nil).response { response in
             switch response.result {
             case .success(let data) :
                 do {
                        print("SuccessFully Registerd")
                         debugPrint(response.response!)
                         let dataResponse = try JSONDecoder().decode(RegisterModel.self, from: data!)
                         self.registerDetail = dataResponse
                 } catch let error {
                     debugPrint(error.localizedDescription)
                 }
             case .failure(let error):
                 print(error)

             }

         }

given your data, use: let isAround: Int?给定您的数据,使用: let isAround: Int?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法读取数据,因为它的格式不正确。” - The data couldn't be read because it isn't in the correct format." 由于错误,JSON 无法序列化:无法读取数据,因为它的格式不正确。[Swift 4.2] - JSON could not be serialized because of error: The data couldn’t be read because it isn’t in the correct format.[Swift 4.2] 在Swift 4中使用新的JSONDecoder协议时,我得到“无法读取数据,因为数据格式不正确。” 错误 - When using the new JSONDecoder protocol in Swift 4 i'm getting the 'the data couldn’t be read because it isn’t in the correct format.' error 无法读取数据,因为它的格式不正确。 错误 - The data couldn’t be read because it isn’t in the correct format. error Swift/MySql/PHP - “无法读取数据,因为它的格式不正确。” 错误 - Swift/MySql/PHP - "The data couldn’t be read because it isn’t in the correct format." error 错误==由于格式不正确,无法读取数据。 当我使用AFNetworking请求 - error==The data couldn’t be read because it isn’t in the correct format. When I use AFNetworking to request 如何解决 Xcode 13.2.1 编译时错误“无法读取数据,因为它的格式不正确。”? - How to resolve Xcode 13.2.1 compile time error "The data couldn’t be read because it isn’t in the correct format."? 无法读取数据,因为它的格式不正确。 Swift 5 - The data couldn’t be read because it isn’t in the correct format. Swift 5 Swift 从 API 解析 json - 无法读取数据,因为它的格式不正确 - Swift parse json from API - The data couldn’t be read because it isn’t in the correct format SWIFT - JSON 错误:无法读取数据,因为它的格式不正确 - SWIFT - JSON error: The data couldn’t be read because it isn’t in the correct format
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM