json - 我有 2 个 json 如何用 1 个结构解码它们?
问题描述
我有两个相似的 JSON 对象,它们唯一的区别是joined_user
. 在第二个joined_user
是[]
。我想使用一个结构来解码它们,我该怎么做?
这是我的第一个 JSON 对象
{
"joined_user":[
{
"_id":"5c72c03cd1c32313908845d8"
},
{
"_id":"5c72c14ad1c32313908845db"
}
],
"allRooms":[
{
"Capacity":{
"Free":10,
"Total":10
},
"Finished":false,
"Pic":"http://192.168.1.101:2525/icon.png",
"_id":"5c72bf3fd1c32313908845d6",
"Coin_input":10,
"Award_money":100000,
"Award_coin":100,
"Match_time":"1551551483",
"Winner_number":1
},
{
"Capacity":{
"Free":20,
"Total":20
},
"Finished":false,
"Pic":"http://192.168.1.101:2525/icon.png",
"_id":"5c72bf58d1c32313908845d7",
"Coin_input":20,
"Award_money":200000,
"Award_coin":200,
"Match_time":"1551551483",
"Winner_number":1
},
{
"Capacity":{
"Free":9,
"Total":10
},
"Finished":false,
"Pic":"http://192.168.1.101:2525/icon.png",
"_id":"5c72c03cd1c32313908845d8",
"Coin_input":100,
"Award_money":100000,
"Award_coin":1000,
"Match_time":"1551551483",
"Winner_number":1
},
{
"Capacity":{
"Free":20,
"Total":20
},
"Finished":false,
"Pic":"http://192.168.1.101:2525/icon.png",
"_id":"5c72c066d1c32313908845d9",
"Coin_input":101,
"Award_money":200000,
"Award_coin":2000,
"Match_time":"1551551483",
"Winner_number":1
},
{
"Capacity":{
"Free":9209269,
"Total":10000000
},
"Finished":true,
"Pic":"http://192.168.1.101:2525/icon.png",
"_id":"5c72c14ad1c32313908845db",
"Coin_input":12,
"Award_money":50000,
"Award_coin":500,
"Match_time":"1551540600",
"Winner_number":1
}
]
}
这是我的第二个 JSON 对象
{
"joined_user":"[]",
"allRooms":[
{
"Capacity":{
"Free":44,
"Total":100
},
"Finished":false,
"Pic":"www",
"_id":"5c73c1666f39db1de8977fd9",
"Coin_input":10,
"Award_money":10000,
"Award_coin":1000,
"Match_time":"9",
"Winner_number":2
},
{
"Capacity":{
"Free":9999,
"Total":9999
},
"Finished":false,
"Pic":"129",
"_id":"5c7535a56f39db1de897802d",
"Coin_input":9,
"Award_money":99999,
"Award_coin":999,
"Match_time":"9",
"Winner_number":9
},
{
"Capacity":{
"Free":9999,
"Total":9999
},
"Finished":false,
"Pic":"129",
"_id":"5c7535a66f39db1de897802e",
"Coin_input":9,
"Award_money":99999,
"Award_coin":999,
"Match_time":"9",
"Winner_number":9
},
{
"Capacity":{
"Free":9999,
"Total":9999
},
"Finished":false,
"Pic":"129",
"_id":"5c7535a86f39db1de897802f",
"Coin_input":9,
"Award_money":99999,
"Award_coin":999,
"Match_time":"9",
"Winner_number":9
}
]
}
解决方案
这是一组用户模型
"joined_user": [ { "_id": "5c72c03cd1c32313908845d8" }, { "_id": "5c72c14ad1c32313908845db" } ]
这是一个字符串
"joined_user":"[]"
要阅读两者,您需要
struct Root: Codable {
let joinedUser: CombinedRes
let allRooms: [AllRoom]
enum CodingKeys: String, CodingKey {
case joinedUser = "joined_user"
case allRooms
}
}
struct AllRoom: Codable {
let capacity: Capacity
let finished: Bool
let pic, id: String
let coinInput, awardMoney, awardCoin: Int
let matchTime: String
let winnerNumber: Int
enum CodingKeys: String, CodingKey {
case capacity = "Capacity"
case finished = "Finished"
case pic = "Pic"
case id = "_id"
case coinInput = "Coin_input"
case awardMoney = "Award_money"
case awardCoin = "Award_coin"
case matchTime = "Match_time"
case winnerNumber = "Winner_number"
}
}
struct Capacity: Codable {
let free, total: Int
enum CodingKeys: String, CodingKey {
case free = "Free"
case total = "Total"
}
}
enum CombinedRes: Codable {
case joinedUserElementArray([JoinedUserElement])
case string(String)
init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
if let x = try? container.decode([JoinedUserElement].self) {
self = .joinedUserElementArray(x)
return
}
if let x = try? container.decode(String.self) {
self = .string(x)
return
}
throw DecodingError.typeMismatch(CombinedRes.self, DecodingError.Context(codingPath: decoder.codingPath, debugDescription: "Wrong type for CombinedRes"))
}
func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
switch self {
case .joinedUserElementArray(let x):
try container.encode(x)
case .string(let x):
try container.encode(x)
}
}
}
struct JoinedUserElement: Codable {
let id: String
enum CodingKeys: String, CodingKey {
case id = "_id"
}
}
但如果你能改变这个
"joined_user": "[]",
至
“加入用户”:[],
那么它将是
struct Root: Codable {
let joinedUser: [JoinedUserElement]
let allRooms: [AllRoom]
enum CodingKeys: String, CodingKey {
case joinedUser = "joined_user"
case allRooms
}
}
或者
"joined_user":null,
那么将是
struct Root: Codable {
let joinedUser: [JoinedUserElement]?
let allRooms: [AllRoom]
enum CodingKeys: String, CodingKey {
case joinedUser = "joined_user"
case allRooms
}
}
do {
let res = try JSONDecoder().decode(Root.self,from:data)
switch(res.joinedUser) {
case .joinedUserElementArray( let arr) :
let ids = arr.map { $0.id }
print(ids)
case .string(let str) :
break
default:
break
}
}
catch {
print(error)
}
如果接收到的值为字符串,您还可以init
在内部编写一个自定义,将空分配给数组Root
推荐阅读
- python - 将带有图像的 PDF 转换为文本
- python - 获取选中的checkbutton-tkinter的文本
- flutter - 如何在 dart 中生成或编译 Flutter gRPC .proto 文件
- angularjs - Angular Js Smart Table自定义过滤器使用空格搜索多词过滤器
- typescript - TypeScript:在数据类型和函数上使用类型参数来配置可变性?
- microservices - 使客户端处理微服务最终一致性的最佳实践
- javascript - 在 vuejs 或 javascript 中合并两个多维数组
- amazon-web-services - 我们可以从 .NET 应用程序访问 AWS Quantum DB (QLDB) 数据库吗?
- npm - 如何修复模块构建失败 - 未捕获的错误:模块构建失败(来自 ./node_modules/babel-loader/lib/index.js):
- c++ - 有什么方法可以检查在 C 中的缓冲区中写入了多少数据/字节