【问题标题】:Recursive call for JSON in swift 3swift 3中对JSON的递归调用
【发布时间】: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


【解决方案1】:

像这样去。

let childrenData = dic["children_data"] as? NSArray
{
for data in childrenData
{
let child_dic = data as! NSDictionary
//Here Your Code.
}
}

您可以对孩子的孩子使用相同的。

【讨论】:

  • 那么我将不得不再次使用 do catch 块进行 jsonSerialization 调用以将元素投射到 child_dic 字典中?或者我应该如何使用我已经解析的对象来为 child_dic 键值对赋值?
  • 你只需要使用。不需要再做jsonSerialization
  • 对不起,我不太明白你在说什么 :( 我知道我会通过数据通过迭代获得所有字典,然后我可以将它转换为字典然后我需要什么怎么做才能再次调用或设置 id、父 id 等的值?
  • let child_id = child_dic["id"] 这样就可以取值了。
猜你喜欢
  • 2017-01-23
  • 1970-01-01
  • 1970-01-01
  • 2017-11-16
  • 1970-01-01
  • 1970-01-01
  • 2020-05-30
  • 2014-11-08
  • 1970-01-01
相关资源
最近更新 更多