尝试在 Swift 上解析 JSON 个数组数组
Try to parse JSON Array of Arrays on Swift
这里是 json 代码:
{
"rows":
[[{
"text":"some text",
"type":{
"type":"inlineUrl",
"url":"https://nothing.com"
}
}],
[{
"text":"some text",
"type":{
"type":"inlineCallback",
"data":"some data"
}
},
{
"text":"some text",
"type":{
"type":"inlineCallback",
"data":"some data"
}
}
]]
}
更缩写的形式如下:
行数 = [ [Row1], [Row2, Row3] ]
我正在努力让它像这样工作:
struct ReplyHandler: Codable {
let rows: [RowsHandler]
enum CodingKeys: String, CodingKey {
case rows
}
}
struct RowsHandler: Codable {
let row1: [RowHandler]
let row2: [Row2Handler]
enum CodingKeys: String, CodingKey {
case row1
case row2
}
}
struct RowHandler: Codable {
let text: String
let type: TypeHandler
enum CodingKeys: String, CodingKey {
case type = "type"
case text = "text"
}
}
struct Row2Handler: Codable {
let row1: RowHandler
let row2: RowHandler
enum CodingKeys: String, CodingKey {
case row1
case row2
}
}
struct TypeHandler: Codable {
let type: String
let url: String
enum CodingKeys: String, CodingKey {
case type = "type"
case url = "url"
}
}
但是 Xcode 编译器给出了这个错误:
Expected to decode String but found an array instead.
不明白如何在数组中正确嵌套其他数组
发问题的时候发现“type”里的数组不一样:第一行有url,第二行和第三行有数据,但总之不是一个点
您可以将模型简化为如下形式:
struct ReplyHandler: Codable {
let rows: [[RowHandler]]
}
struct RowHandler: Codable {
let text: String
let type: TypeHandler
}
struct TypeHandler: Codable {
let type: String
var url: String?
var data: String?
}
请注意 TypeHandler
中的 url
和 data
属性是涵盖这两种情况的可选属性。
虽然手动 JSON 解析很有用我认为使用这样的工具 https://app.quicktype.io/ 将 JSON 转换为 Swift 类 或结构
为您JSON它提供了这个结果
// MARK: - Welcome
struct Welcome: Codable {
let rows: [[Row]]
}
// MARK: - Row
struct Row: Codable {
let text: String
let type: TypeClass
}
// MARK: - TypeClass
struct TypeClass: Codable {
let type: String
let url: String?
let data: String?
}
这里是 json 代码:
{
"rows":
[[{
"text":"some text",
"type":{
"type":"inlineUrl",
"url":"https://nothing.com"
}
}],
[{
"text":"some text",
"type":{
"type":"inlineCallback",
"data":"some data"
}
},
{
"text":"some text",
"type":{
"type":"inlineCallback",
"data":"some data"
}
}
]]
}
更缩写的形式如下: 行数 = [ [Row1], [Row2, Row3] ]
我正在努力让它像这样工作:
struct ReplyHandler: Codable {
let rows: [RowsHandler]
enum CodingKeys: String, CodingKey {
case rows
}
}
struct RowsHandler: Codable {
let row1: [RowHandler]
let row2: [Row2Handler]
enum CodingKeys: String, CodingKey {
case row1
case row2
}
}
struct RowHandler: Codable {
let text: String
let type: TypeHandler
enum CodingKeys: String, CodingKey {
case type = "type"
case text = "text"
}
}
struct Row2Handler: Codable {
let row1: RowHandler
let row2: RowHandler
enum CodingKeys: String, CodingKey {
case row1
case row2
}
}
struct TypeHandler: Codable {
let type: String
let url: String
enum CodingKeys: String, CodingKey {
case type = "type"
case url = "url"
}
}
但是 Xcode 编译器给出了这个错误:
Expected to decode String but found an array instead.
不明白如何在数组中正确嵌套其他数组
发问题的时候发现“type”里的数组不一样:第一行有url,第二行和第三行有数据,但总之不是一个点
您可以将模型简化为如下形式:
struct ReplyHandler: Codable {
let rows: [[RowHandler]]
}
struct RowHandler: Codable {
let text: String
let type: TypeHandler
}
struct TypeHandler: Codable {
let type: String
var url: String?
var data: String?
}
请注意 TypeHandler
中的 url
和 data
属性是涵盖这两种情况的可选属性。
虽然手动 JSON 解析很有用我认为使用这样的工具 https://app.quicktype.io/ 将 JSON 转换为 Swift 类 或结构
为您JSON它提供了这个结果
// MARK: - Welcome
struct Welcome: Codable {
let rows: [[Row]]
}
// MARK: - Row
struct Row: Codable {
let text: String
let type: TypeClass
}
// MARK: - TypeClass
struct TypeClass: Codable {
let type: String
let url: String?
let data: String?
}