json - 你如何在 Swift 中解析递归 JSON?
问题描述
我从数据是递归的服务器接收 JSON。将其解析为方便的 Swift 数据结构的最佳方法是什么?
定义 Swift Codable 数据结构以将其解析为失败,因为不允许使用递归属性。
Swift 编译器报告:“值类型 'FamilyTree.Person' 不能具有递归包含它的存储属性”
{
"familyTree": {
"rootPerson": {
"name": "Tom",
"parents": {
"mother": {
"name": "Ma",
"parents": {
"mother": {
"name": "GraMa",
"parents": {}
},
"father": {
"name": "GraPa",
"parents": {}
}
}
},
"father": {
"name": "Pa",
"parents": {}
}
}
}
}
}
理想情况下,最终结果是一堆从 rootPerson 对象开始指向它们的父对象和父对象的人对象。
解决方案
第一个想法是创建结构,例如:
struct Person: Codable {
var name: String
var parents: Parents
}
struct Parents: Codable {
var mother: Person?
var father: Person?
}
但这不起作用,因为您不能拥有像这样的递归存储属性。
这是一种可能的工作解决方案:
let json = """
{
"familyTree": {
"rootPerson": {
"name": "Tom",
"parents": {
"mother": {
"name": "Ma",
"parents": {
"mother": {
"name": "GraMa",
"parents": {}
},
"father": {
"name": "GraPa",
"parents": {}
}
}
},
"father": {
"name": "Pa",
"parents": {}
}
}
}
}
}
"""
struct Person: Codable {
var name: String
var parents: [String: Person]
}
struct FamilyTree: Codable {
var rootPerson: Person
}
struct Root: Codable {
var familyTree: FamilyTree
}
let decoder = JSONDecoder()
let tree = try decoder.decode(Root.self, from: json.data(using: .utf8)!)
print(tree)
在操场上,这将正确解析 JSON。
的parents
字典Person
将具有 和 等"mother"
键"father"
。这支持一个人有任意数量的父母和任何角色。
推荐阅读
- javascript - 错误:将文件上传到服务器时流意外结束(反应本机 fs)
- ios - 想要将视图控制器添加到标签栏项目
- angular - 选择 mat-option 后角度更新 formControl 值
- php - 在 PHP 中将 MySQL 结果返回到 GraphQL
- php - 如何使用 PHP 从 Google Team Drive 中获取所有照片?
- three.js - 来自 youtube 的 3D 场景中的threejs 视频
- linux - 如何将外部传入流量从一个端口重定向到另一个端口?(Linux/iptables/ufw)
- java - Maven插件编译正常但在执行期间找不到类
- kotlin - 如果 KParameter.isOptional = true,我如何获得默认值?
- spring - 当 Springboot 中的 Schedulers.elastic 时,Webflux 不传播异常