Ios 解码具有不同类型的Json对象
我有一个用于填充表视图的Json数据。 Json在其主要部分有一种和两种类型的对象,即“main”和n个tableview对象 主对象中有两种字符串类型(“url”和“bar\u name”)。Tableview对象有3种类型(“名称”、“id”和[tableData])。这对于所有tableview对象都是通用的(n个)。填充单元格的Tabledata对象有3种不同的类型,因为一个打开url,另一个打开新表,最后一个只执行一个操作 我想将这种类型的json解码为结构。可能吗Ios 解码具有不同类型的Json对象,ios,json,swift,decode,codable,Ios,Json,Swift,Decode,Codable,我有一个用于填充表视图的Json数据。 Json在其主要部分有一种和两种类型的对象,即“main”和n个tableview对象 主对象中有两种字符串类型(“url”和“bar\u name”)。Tableview对象有3种类型(“名称”、“id”和[tableData])。这对于所有tableview对象都是通用的(n个)。填充单元格的Tabledata对象有3种不同的类型,因为一个打开url,另一个打开新表,最后一个只执行一个操作 我想将这种类型的json解码为结构。可能吗 //For
//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
}
编辑:我找到了一个类似的帖子,但不适合我的情况。我在犯错误
类型“table”不符合协议“Encodable”
EDIT2:我添加了解码代码,但主页和用户的代码都是零。我的结构缺少什么
let data = try Data(contentsOf: URL(fileURLWithPath: path), options: .alwaysMapped)
let decoder = JSONDecoder()
let admin = try! decoder.decode(User.self, from: data)
print(admin)
您应该实现用于解码的自定义初始值设定项和用于编码的
编码(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
}
我需要为管理员创建结构吗?json的最高级别是哪一级?顺便说一下,我有一个被调用的表,它是一个表数组,但在json中它被称为表1、表2。如何更改它以进行解码?@EmreO。是,如果您想维护所有JSON响应树,请编辑我的问题。你能检查一下吗。非常感谢你。@EmreO。您还需要为每个命名不同的模型变量指定键名。我强烈建议您阅读本指南:
User
包含一个字典,Table1
也包含一个字典,只有单元格
包含一个数组。请仔细阅读输出。JSON只有两种集合类型。区分它们很简单<代码>[]是数组{}
是dictionary@vadian我编辑我的问题。你能检查一下吗?非常感谢。再次阅读JSON。你必须学会理解结构。没有键表
,并且该值不是数组。和案件敏感性问题<代码>用户获得了主页
,表1
和表2
我现在明白了。我正在努力。非常感谢。