【发布时间】:2017-08-01 09:54:31
【问题描述】:
我的回复看起来像这样
{
"id": 3,
"parent_id": 1,
"name": "QuickStart Intelligence",
"is_active": true,
"position": 2,
"level": 1,
"product_count": 590,
"children_data": [
{
"id": 53,
"parent_id": 3,
"name": "Find Training",
"is_active": true,
"position": 1,
"level": 2,
"product_count": 9,
"children_data": [
{
"id": 58,
"parent_id": 53,
"name": "Career Paths",
"is_active": true,
"position": 1,
"level": 3,
"product_count": 0,
"children_data": [
{
"id": 123,
"parent_id": 58,
"name": "Business Productivity",
"is_active": true,
"position": 1,
"level": 4,
"product_count": 245,
"children_data": []
},
{
"id": 70,
"parent_id": 58,
"name": "Creative & Design",
"is_active": true,
"position": 3,
"level": 4,
"product_count": 43,
"children_data": []
},
]
},
{
"id": 57,
"parent_id": 53,
"name": "Technology",
"is_active": true,
"position": 2,
"level": 3,
"product_count": 1,
"children_data": [
{
"id": 162,
"parent_id": 57,
"name": "Microsoft",
"is_active": true,
"position": 6,
"level": 4,
"product_count": 183,
"children_data": []
},
{
"id": 164,
"parent_id": 57,
"name": "Adobe",
"is_active": true,
"position": 8,
"level": 4,
"product_count": 34,
"children_data": []
}
]
}
{
"id": 100,
"parent_id": 53,
"name": "Certifications",
"is_active": true,
"position": 3,
"level": 3,
"product_count": 0,
"children_data": [
{
"id": 112,
"parent_id": 100,
"name": "Microsoft SQL Server 2012",
"is_active": true,
"position": 1,
"level": 4,
"product_count": 0,
"children_data": []
},
{
"id": 113,
"parent_id": 100,
"name": "Windows Server 2012 Microsoft Certifications",
"is_active": true,
"position": 2,
"level": 4,
"product_count": 0,
"children_data": []
},
{
"id": 114,
"parent_id": 100,
"name": "Visual Studio Microsoft Certifications",
"is_active": true,
"position": 3,
"level": 4,
"product_count": 0,
"children_data": []
},
{
"id": 115,
"parent_id": 100,
"name": "SharePoint 2013 Microsoft Certifications",
"is_active": true,
"position": 4,
"level": 4,
"product_count": 0,
"children_data": []
},
{
"id": 116,
"parent_id": 100,
"name": "Private Cloud Microsoft Certifications",
"is_active": true,
"position": 5,
"level": 4,
"product_count": 0,
"children_data": []
},
{
"id": 117,
"parent_id": 100,
"name": "Exchange Server 2013 Certifications",
"is_active": true,
"position": 6,
"level": 4,
"product_count": 0,
"children_data": []
},
{
"id": 118,
"parent_id": 100,
"name": "Lync Server 2013 Microsoft Certifications",
"is_active": true,
"position": 7,
"level": 4,
"product_count": 0,
"children_data": []
},
{
"id": 119,
"parent_id": 100,
"name": "Windows 8 Microsoft Certifications",
"is_active": true,
"position": 8,
"level": 4,
"product_count": 0,
"children_data": []
},
{
"id": 120,
"parent_id": 100,
"name": "Microsoft System Center Certifications",
"is_active": true,
"position": 9,
"level": 4,
"product_count": 0,
"children_data": []
},
{
"id": 121,
"parent_id": 100,
"name": "Microsoft Windows Server 2008 Certifications",
"is_active": true,
"position": 10,
"level": 4,
"product_count": 0,
"children_data": []
}
]
}
就这样继续下去。我已经像这样在我的类类别中初始化了一些变量
var categoryId = Int()
var parentId = Int()
var categoryName = String()
var isCategoryActive = Bool()
var categoryPostion = Int()
var categoryLevel = Int()
var categoryChildCount = Int()
var childArray = [Categories]()
我知道它应该通过递归调用来完成,但不知何故我无法解决这个问题。我试过查看溢出但找不到任何相关的东西。这就是我投射 json 的方式
class func getUserFromJson(_ jsonString: String) -> Categories {
let category = Categories()
do {
let dic = try JSONSerialization.jsonObject(with: jsonString.data(using: String.Encoding(rawValue: String.Encoding.utf8.rawValue))!, options: .allowFragments) as! NSDictionary
if let id = dic.object(forKey: "id") as? Int {
category.categoryId = id
}
if let parentId = dic.object(forKey: "parent_id") as? Int {
category.parentId = parentId
}
if let name = dic.object(forKey: "name") as? String {
category.categoryName = name
}
if let isActive = dic.object(forKey: "is_active") as? Bool {
category.isCategoryActive = isActive
}
if let position = dic.object(forKey: "position") as? Int {
category.categoryPostion = position
}
if let level = dic.object(forKey: "level") as? Int {
category.categoryLevel = level
}
if let productCount = dic.object(forKey: "product_count") as? Int {
category.categoryChildCount = productCount
}
}
catch {
}
return category
}
我将如何解析 children_data,它是一个数组并且有很多孩子,而这些孩子又有孩子,正如你所看到的,其中一些是空的,所以我也需要检查一下。
【问题讨论】:
-
复制你的 json 数据并使用 json viewer 。它可以很容易地解析你的数据jsonviewer.stack.hu
标签: json recursion swift3 xcode8