gpt4 book ai didi

ios - 解码具有不同类型的 Json 对象

转载 作者:行者123 更新时间:2023-11-28 07:56:20 26 4
gpt4 key购买 nike

我有一个用于填充 TableView 的 Json 数据。Json 的主要部分有两种类型的对象,分别是“Main”和 n-number tableview 对象;

主对象中有两个字符串类型(“url”和“bar_name”)。 Tableview 对象有 3 种类型(“name”、“id”、[tableData])。这对于所有 tableview 对象(n 个)都是常见的。填充单元格的 Tabledata 对象有 3 种不同的类型,因为一种打开一个 url,另一种打开一个新表,最后一个只做一个 Action 。

我想将这种类型的 json 解码为结构。可能吗?

    //For All Types
protocol commonCellTypes: Codable{
var type: Int { get }

}
struct User: Codable{
let mainPage: MainPage?
let tables: [table]?
}
struct MainPage: Codable{
let url: String?
let bar_name: String?
}
struct table: Codable{
let name: String?
let id: String?
let Cells: [commonCellTypes]

enum CodeKeys: CodingKey
{
case name
case id
case tableDatas
}

init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodeKeys.self)
name = try container.decode (String.self, forKey: .name)
id = try container.decode (String.self, forKey: .id)
Cells = try container.decode ([commonCellTypes].self, forKey: .tableDatas)
}

func encode(to encoder: Encoder) throws
{
var container = encoder.container(keyedBy: CodeKeys.self)
try container.encode (name, forKey: .name)
try container.encode (id, forKey: .id)
try container.encode (Cells, forKey: .tableDatas)
}
}
//Type 2
struct cellType2: commonCellTypes{
let cellText: String
let imageName: String?
let url: URL?
let type: Int
}
//Type 1
struct cellType1: commonCellTypes{
let cellText: String
let imageName: String
let table_id: String
let type: Int
}
//Type 0
struct cellType0: commonCellTypes{
let cellText: String
let imageName: String
let type: Int
let action: String
}

enter image description here

编辑:我找到了一个类似的帖子,但不适用于我的情况。我遇到错误

Type ' table' does not conform to protocol 'Encodable'

Swift JSONDecoder with multiple structures

EDIT2:我添加了解码代码,但 mainPage 和 User 都为零。我的结构缺少什么?

  let data = try Data(contentsOf: URL(fileURLWithPath: path), options: .alwaysMapped)
let decoder = JSONDecoder()
let admin = try! decoder.decode(User.self, from: data)
print(admin)

最佳答案

您应该为解码实现自定义初始化器,为编码实现encode(to encoder: Encoder)。试试这个:

//For All Types
protocol commonCellTypes: Codable{

}
struct MainStruct: Codable{
let tables: [table]
}
struct table: Codable{
let name: String?
let id: String?
let tableDatas: [commonCellTypes]

enum CodeKeys: CodingKey
{
case name
case id
case tableDatas
}

init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodeKeys.self)
name = try container.decode (String.self, forKey: .name)
id = try container.decode (String.self, forKey: .id)
tableDatas = try container.decode ([commonCellTypes].self, forKey: .tableDatas)
}

func encode(to encoder: Encoder) throws
{
var container = encoder.container(keyedBy: CodeKeys.self)
try container.encode (name, forKey: .name)
try container.encode (id, forKey: .id)
try container.encode (tableDatas, forKey: .tableDatas)
}
}
//Type 2
struct cellType2: commonCellTypes{
let cellText: String
let imageName: String?
let url: URL?
let type: Int
}
//Type 1
struct cellType1: commonCellTypes{
let cellText: String
let imageName: String
let table_id: String
let type: Int
}
//Type 0
struct cellType0: commonCellTypes{
let cellText: String
let imageName: String
let type: Int
let action: String
}

关于ios - 解码具有不同类型的 Json 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47940075/

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