gpt4 book ai didi

json - 难以使用 Codeable 从 JSON 中提取数据

转载 作者:可可西里 更新时间:2023-11-01 01:07:27 25 4
gpt4 key购买 nike

我正在尝试从下面的 JSON 中提取 “数据”:

{
"result": "success",
"code": 200,
"version": "1",
"time": 1,
"id": "NODATA",
"data": {
"user_address_town": {
"label": "town",
"required": true,
"type": "string",
"value": ""
},
"user_address_county": {
"label": "county",
"required": true,
"type": "string"
},
"user_address_line_1": {
"label": "address 1",
"required": true,
"type": "string",
"value": ""
},
"user_address_line_2": {
"label": "address 2",
"required": false,
"type": "string",
"value": ""
},
"user_address_line_3": {
"label": "address 3",
"required": false,
"type": "string",
"value": ""
},
"user_address_postcode": {
"label": "postcode",
"required": false,
"type": "string",
"value": ""
},
"user_address_identifier": {
"label": "identifier",
"required": false,
"type": "string",
"value": ""
},
"stage": "stage1",
"last": true,
"ekyc": true,
"email": {
"label": "email address",
"required": true,
"type": "email",
"value": "test+15@mailinator.com"
},
"password": {
"label": "password",
"required": true,
"type": "string"
},
"user_title": {
"label": "title",
"required": true,
"type": "string",
"value": "Mrs"
},
"user_first_name": {
"label": "first name",
"required": true,
"type": "string",
"value": "jsns"
},
"user_last_name": {
"label": "last name",
"required": true,
"type": "string",
"value": "nsm"
},
"user_dob": {
"label": "user date of birth",
"type": "date",
"required": true,
"value": "2001-01-22"
},
"user_gender": {
"label": "gender",
"type": "string",
"required": false,
"value": "M"
},
"user_country": {
"label": "country",
"required": true,
"type": "integer",
"value": "1"
},
"user_telephone": {
"label": "telephone",
"required": false,
"type": "string",
"value": "946464646"
},
"user_name_on_card": {
"label": "card name",
"required": false,
"type": "string"
}
}
}

我创建了一个名为 IncompleteSignupResponse 的结构,解码后它应该包含 "data" 对象中的所有数据。 userObject 数组用于数据中较小的对象,例如user_address_town

以下是我正在使用的结构:

struct IncompleteSignupResponse {
let stage: String
let last: Bool
let ekyc: Bool
let error: String?
let userObject: [IncompleteUserObjectResponse]

}

struct IncompleteUserObjectResponse: Codable {
let label: String
let required: Bool
let type: String
let value: String
}

extension IncompleteSignupResponse: Codable {

private enum IncompleteSignupResponseAPIResponseCodingKeys: String, CodingKey {
case data = "data"
}

init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: IncompleteSignupResponseAPIResponseCodingKeys.self)
let data = try container.decode(IncompleteSignupResponse.self, forKey: .data)
stage = data.stage
last = data.last
ekyc = data.ekyc
error = data.error
userObject = data.userObject
}
}

尝试解码该结构会给出以下响应:

keyNotFound(IncompleteSignupResponseAPIResponseCodingKeys(stringValue: "data", intValue: nil), Swift.DecodingError.Context(codingPath: [IncompleteSignupResponseAPIResponseCodingKeys(stringValue: "data", intValue: nil)], debugDescription: "No value associated with key IncompleteSignupResponseAPIResponseCodingKeys(stringValue: \"data\", intValue: nil) (\"data\").", underlyingError: nil))

我不太确定为什么找不到关键数据,因为它没有嵌套在任何东西中..

有什么我遗漏的吗?另外,我的布局是否被认为是好的做法,或者是否有可以改进的地方。

最佳答案

使用本网站:JSONParser .您可以在您的代码中使用此结构:

struct Welcome: Codable {
let result: String
let code: Int
let version: String
let time: Int
let id: String
let data: DataClass
}

struct DataClass: Codable {
let userAddressTown, userAddressCounty, userAddressLine1, userAddressLine2: Email
let userAddressLine3, userAddressPostcode, userAddressIdentifier: Email
let stage: String
let last, ekyc: Bool
let email, password, userTitle, userFirstName: Email
let userLastName, userDob, userGender, userCountry: Email
let userTelephone, userNameOnCard: Email

enum CodingKeys: String, CodingKey {
case userAddressTown = "user_address_town"
case userAddressCounty = "user_address_county"
case userAddressLine1 = "user_address_line_1"
case userAddressLine2 = "user_address_line_2"
case userAddressLine3 = "user_address_line_3"
case userAddressPostcode = "user_address_postcode"
case userAddressIdentifier = "user_address_identifier"
case stage, last, ekyc, email, password
case userTitle = "user_title"
case userFirstName = "user_first_name"
case userLastName = "user_last_name"
case userDob = "user_dob"
case userGender = "user_gender"
case userCountry = "user_country"
case userTelephone = "user_telephone"
case userNameOnCard = "user_name_on_card"
}
}

struct Email: Codable {
let label: String
let emailRequired: Bool
let type: TypeEnum
let value: String?

enum CodingKeys: String, CodingKey {
case label
case emailRequired = "required"
case type, value
}
}

enum TypeEnum: String, Codable {
case date = "date"
case email = "email"
case integer = "integer"
case string = "string"
}

关于json - 难以使用 Codeable 从 JSON 中提取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55101292/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com