gpt4 book ai didi

ios - 在 Swift 4 上解码 JSON 数据返回 nil

转载 作者:行者123 更新时间:2023-11-30 11:57:10 25 4
gpt4 key购买 nike

我正在尝试使用以下函数解码我的 json 数据。我一开始有不同的 Json 格式,它可以工作,但是当我更改 json 格式时,它开始不起作用。我也尝试在结构代码中进行更改,但没有任何效果。我缺少什么? (代码适用于第一种 Json 格式,因此也需要适用于第二种格式)

第一个 JSON:

{
"status": 200,
"results": [
{
"group_matched": false,
"distance_mi": 4,
"content_hash": "1rT2lirUapfrYIYxSR2u0GtmRSLPsVki9kFj4ugs8JIq6",
"common_friends": [],
"common_likes": [],
"common_friend_count": 0,
"common_like_count": 0,
"connection_count": 0,
"_id": "5a2805ba779f34267d32d8b0",
"bio": "",
"birth_date": "1997-12-09T16:28:29.761Z",
"name": "John",
},

第二个 JSON:

{
"meta": {
"status": 200
},
"data": {
"results": [
{
"type": "user",
"user": {
"_id": "545001b33bf179416a30bf7f",
"bio": "Ä°nstagram:",
"birth_date": "1992-12-09T17:12:49.957Z",
"name": "",
"photos": [
{
"id": "2eb1beec-6180-4d58-90fd-5f076da96af9",
"url": "",
"processedFiles": [
{
"url": "",
"height": 640,
"width": 640
},
{
"url": "",
"height": 320,
"width": 320
},
{
"url": "",
"height": 172,
"width": 172
},
{
"url": "",
"height": 84,
"width": 84
}
]
},
{
"id": "89d2bc07-d244-457b-b610-26ecd4e9e86d",
"url": "",
"processedFiles": [
{
"url": "",
"height": 640,
"width": 640
},
{
"url": "",
"height": 320,
"width": 320
},
{
"url": "",
"height": 172,
"width": 172
},
{
"url": "",
"height": 84,
"width": 84
}
]
},
{
"id": "0fcafc7e-d6e7-4cc5-891a-735971d6a5b2",
"url": "",
"processedFiles": [
{
"url": "",
"height": 640,
"width": 640
},
{
"url": "g",
"height": 320,
"width": 320
},
{
"url": "",
"height": 172,
"width": 172
},
{
"url": "",
"height": 84,
"width": 84
}
]
},
{
"id": "bac74531-09e7-4c5b-ac6c-cab36c4be587",
"url": "",
"processedFiles": [
{
"url": "",
"height": 640,
"width": 640
},
{
"url": "",
"height": 320,
"width": 320
},
{
"url": "",
"height": 172,
"width": 172
},
{
"url": "",
"height": 84,
"width": 84
}
]
},
{
"id": "3c216244-946a-4c58-8670-cc12c05801cb",
"url": "",
"processedFiles": [
{
"url": "",
"height": 640,
"width": 640
},
{
"url": "",
"height": 320,
"width": 320
},
{
"url": "",
"height": 172,
"width": 172
},
{
"url": "",
"height": 84,
"width": 84
}
]
},
{
"id": "-8672-488d-8b7e-f30b4560c56b",
"url": "",
"processedFiles": [
{
"url": "",
"height": 640,
"width": 640
},
{
"url": "",
"height": 320,
"width": 320
},
{
"url": "",
"height": 172,
"width": 172
},
{
"url": "",
"height": 84,
"width": 84
}
]
}
],
"gender": 1,

结构代码:

    struct Photo: Codable{
var url: String?
var processedFiles: [processedFiles]?
}
struct User: Codable{
var group_matched: Bool?
var distance_mi: Int?
var common_friend_count: Int?
var name:String?
var profile_picture: String?
var instagram_id: String?
var photos: [Photo]?
}
class Results : Codable {
var results: [User] = []
static let sharedResults = Results()
private init() { }
var type: String?

func populateData(sender: Results){
print(sender)
results += sender.results
}
}

解码代码:

 let jsonData = JSON(data: response.data!)
let jsonData2 = try? JSONSerialization.data(withJSONObject: jsonData["data"].object)
print(JSON(data: jsonData2!))

let decoder = JSONDecoder()
var response_class = Results.sharedResults
response_class = try decoder.decode(Results.self, from: jsonData2!)
Results.sharedResults.populateData(sender: response_class)

编辑:我编辑第二个 JSON,它现在有数组的数组。如何为第二个数组实现解码器 key ?

最佳答案

在这种情况下,我将使用 nestedContainer 编写一个自定义初始值设定项,以将 user 字典包含到父对象中

let jsonString = """
{
"meta": {
"status": 200
},
"data": {
"results": [
{
"type": "user",
"user": {
"_id": "545001b33bf179416a30bf7f",
"bio": "Ä°nstagram:hello",
"birth_date": "1992-12-09T17:12:49.957Z",
"name": "Hello"
},

"group_matched": false,
"distance_mi": 4
}
]
}
}
"""
<小时/>
struct Root : Decodable {
let meta : [String:Int]
let data : Result
}

struct Result : Decodable {
let results : [User]
}

struct User : Decodable {

let type : String
let groupMatched : Bool
let distanceMi : Int

let id, bio, birthDate, name : String

private enum CodingKeys : String, CodingKey {
case type, user, groupMatched = "group_matched", distanceMi = "distance_mi"
}

private enum UserKeys: String, CodingKey {
case id = "_id", bio, birthDate = "birth_date", name
}

init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
type = try container.decode(String.self, forKey: .type)
groupMatched = try container.decode(Bool.self, forKey: .groupMatched)
distanceMi = try container.decode(Int.self, forKey: .distanceMi)

let userInfo = try container.nestedContainer(keyedBy: UserKeys.self, forKey: .user)
id = try userInfo.decode(String.self, forKey: .id)
bio = try userInfo.decode(String.self, forKey: .bio)
birthDate = try userInfo.decode(String.self, forKey: .birthDate)
name = try userInfo.decode(String.self, forKey: .name)
}
}
<小时/>
do {
let data = Data(jsonString.utf8)
let decoder = JSONDecoder()
let root = try decoder.decode(Root.self, from: data)
print(root)
} catch {
print("error: ", error)
}

关于ios - 在 Swift 4 上解码 JSON 数据返回 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47679368/

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