Swift 可解码 JSON 处理数据
Swift Decodable JSON Handle data
我正在尝试使用 swift 可编码解码 JSON,但我在如何处理数据方面遇到问题,特别是因为结构是动态的,所以我使用了枚举来解析 JSON.
解析数据后,我尝试使用数组映射,但出现错误
这是一个 JSON 示例(如果您需要完整版本 here
URL) :
{
"kind": "Listing",
"data": {
"after": null,
"dist": 1,
"modhash": "",
"geo_filter": "",
"children": [
{
"kind": "t1",
"data": {
"title": "Test",
"score": 88,
"replies": {
"kind": "Listing",
"data": {
"after": null,
"dist": null,
"modhash": "",
"geo_filter": "",
"children": [
{
"kind": "t1",
"data": {
"title": "Test",
"score": 88
}
},
{
"kind": "t1",
"data": {
"title": "post",
"score": 12
}
}
]
}
}
}
}
]
}
}
这是我解析 JSON
的方式
import Foundation
enum DataType : String, Decodable {
case listing = "Listing"
case t1
case t3
case more
}
indirect enum Parse : Decodable {
case listing(Listing)
case t1(Comment)
case t3(Comment)
case more(More)
enum CodingKeys : String, CodingKey {
case kind
case data
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
let kind = try container.decode(DataType.self, forKey: .kind)
switch kind {
case .listing:
self = .listing(try container.decode(Listing.self, forKey: .data))
case .t1:
self = .t1(try container.decode(Comment.self, forKey: .data))
case .t3:
self = .t3(try container.decode(Comment.self, forKey: .data))
case .more:
self = .more(try container.decode(More.self, forKey: .data))
}
}
}
我正在使用一个模型,所以 model.listings
应该包含在下面的代码中解析的结果数据
let decoder = JSONDecoder()
var result = try decoder.decode([Parse].self, from: data!)
self.listings = result
在终端中,如果我 PO model.listings
数据存在
这是一个例子
▿ 2 elements
▿ 0 : Parse
▿ listing : Listing
▿ paginator : Paginator
▿ after : Optional<String>
- some : ""
▿ before : Optional<String>
- some : ""
▿ modhash : Optional<String>
- some : ""
▿ children : 1 element
▿ 0 : Parse
▿ t3 : Comment
▿ id : 51C7640E-D382-467B-9A65-6CAB04FA470E
- uuid : "51C7640E-D382-467B-9A65-6CAB04FA470E"
▿ title : Optional<String>
- some : "test"
▿ score : Optional<Int>
- some : 88
- replies : nil
我的问题是我不知道如何处理这些数据
我试过类似的东西:
var test = model.listings.map( { [=16=].listing } )
但是它说“枚举大小写 'listing' 不能用作实例成员”
也试过这种方法:
var test = model.listings.listing[0].children
在这种情况下,错误是:“[Parse]”类型的值没有成员 'listing'
我刚开始使用 swift,所以我现在真的很困惑,处理这种数据的最佳方法是什么?
尝试使用以下机制之一:
// Using a Switch to enumerate the possibilities and using a pattern to extract the associated data
switch result {
case .listing(let aListing) :
print(aListing.geo_filter)
case .t1(let comment) :
print(comment.title)
case .t3(let comment) :
print(comment.title)
}
// Using a if case with a pattern to extract one particular enum case
if case .listing(let aListing) = result {
debugPrint(aListing)
}
我正在尝试使用 swift 可编码解码 JSON,但我在如何处理数据方面遇到问题,特别是因为结构是动态的,所以我使用了枚举来解析 JSON.
解析数据后,我尝试使用数组映射,但出现错误
这是一个 JSON 示例(如果您需要完整版本 here URL) :
{
"kind": "Listing",
"data": {
"after": null,
"dist": 1,
"modhash": "",
"geo_filter": "",
"children": [
{
"kind": "t1",
"data": {
"title": "Test",
"score": 88,
"replies": {
"kind": "Listing",
"data": {
"after": null,
"dist": null,
"modhash": "",
"geo_filter": "",
"children": [
{
"kind": "t1",
"data": {
"title": "Test",
"score": 88
}
},
{
"kind": "t1",
"data": {
"title": "post",
"score": 12
}
}
]
}
}
}
}
]
}
}
这是我解析 JSON
的方式import Foundation
enum DataType : String, Decodable {
case listing = "Listing"
case t1
case t3
case more
}
indirect enum Parse : Decodable {
case listing(Listing)
case t1(Comment)
case t3(Comment)
case more(More)
enum CodingKeys : String, CodingKey {
case kind
case data
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
let kind = try container.decode(DataType.self, forKey: .kind)
switch kind {
case .listing:
self = .listing(try container.decode(Listing.self, forKey: .data))
case .t1:
self = .t1(try container.decode(Comment.self, forKey: .data))
case .t3:
self = .t3(try container.decode(Comment.self, forKey: .data))
case .more:
self = .more(try container.decode(More.self, forKey: .data))
}
}
}
我正在使用一个模型,所以 model.listings
应该包含在下面的代码中解析的结果数据
let decoder = JSONDecoder()
var result = try decoder.decode([Parse].self, from: data!)
self.listings = result
在终端中,如果我 PO model.listings
数据存在
这是一个例子
▿ 2 elements
▿ 0 : Parse
▿ listing : Listing
▿ paginator : Paginator
▿ after : Optional<String>
- some : ""
▿ before : Optional<String>
- some : ""
▿ modhash : Optional<String>
- some : ""
▿ children : 1 element
▿ 0 : Parse
▿ t3 : Comment
▿ id : 51C7640E-D382-467B-9A65-6CAB04FA470E
- uuid : "51C7640E-D382-467B-9A65-6CAB04FA470E"
▿ title : Optional<String>
- some : "test"
▿ score : Optional<Int>
- some : 88
- replies : nil
我的问题是我不知道如何处理这些数据
我试过类似的东西:
var test = model.listings.map( { [=16=].listing } )
但是它说“枚举大小写 'listing' 不能用作实例成员”
也试过这种方法:
var test = model.listings.listing[0].children
在这种情况下,错误是:“[Parse]”类型的值没有成员 'listing'
我刚开始使用 swift,所以我现在真的很困惑,处理这种数据的最佳方法是什么?
尝试使用以下机制之一:
// Using a Switch to enumerate the possibilities and using a pattern to extract the associated data
switch result {
case .listing(let aListing) :
print(aListing.geo_filter)
case .t1(let comment) :
print(comment.title)
case .t3(let comment) :
print(comment.title)
}
// Using a if case with a pattern to extract one particular enum case
if case .listing(let aListing) = result {
debugPrint(aListing)
}