【问题标题】:SwiftyJSON - How to loop through child nodes when parsingSwiftyJSON - 解析时如何遍历子节点
【发布时间】:2016-02-17 18:44:54
【问题描述】:

我尝试使用 SwiftyJSON 解析以下 JSON 结构,但我不知道如何循环遍历“sections”节点。我目前拥有的代码返回每个部分的第一个部分节点,但我无法访问“Section 1b / Section 2b”元素。

{
"category": [{
    "categoryId": 1,
    "categoryHeader": "Category 1",
    "sections": [{
        "section": "Section 1a title",
        "body": "Section 1a body"
    }, {
        "section": "Section 1b title",
        "body": "Section 1b body"
    }]
}, {
    "categoryId": 2,
    "categoryHeader": "Category 2",
    "sections": [{
        "section": "Section 2a title",
        "body": "Section 2a body"
    },{
        "section": "Section 2a title",
        "body": "Section 2b body"
    }]
}]

}

我的代码是:

let path: String = NSBundle.mainBundle().pathForResource("jsonFile", ofType: "json") as String!
let data = NSData(contentsOfFile: path) as NSData!

var objects = [[String: String]]()
let json = JSON(data: data)

 func parseJSON(json: JSON) {
    for category in json["category"].arrayValue {
        let categoryHeader = category["categoryHeader"].stringValue
        let section = category["sections"][0]["section"].stringValue
        let sectionBody = category["sections"][0]["body"].stringValue
        let obj = ["categoryHeader": categoryHeader, "section": section, "body": sectionBody] //, "body": body, "title": sigs]

    objects.append(obj)
    }
    tableView.reloadData()
}

我知道我需要遍历 section 部分来检索所有值,但我不知道该怎么做。

编辑: 尝试使用以下内容允许我访问所有部分节点;但随后 'categoryHeader' 输出 'x' 次,具体取决于有多少部分:

func parseJSON(json: JSON) {
    for category in json["category"].arrayValue {
        let categoryHeader = category["categoryHeader"].stringValue

        for section in category["sections"].arrayValue {
            let sectionName = section["section"].stringValue
            let body = section["body"].stringValue
            let obj = ["categoryHeader": categoryHeader, "section": sectionName, "body": body]   
    objects.append(obj)
        }
    }
}

【问题讨论】:

    标签: ios json swift parsing swifty-json


    【解决方案1】:

    一种方法可能是这样的。

    编辑:我有这段代码在操场上工作。

    var objects = [[String: String]]()
    
    
    func parseJSON(json: JSON) {
    
        let jsonResults = try NSJSONSerialization.JSONObjectWithData(data!, options: .MutableContainers) as! NSDictionary
        let json = JSON.self(jsonResults)
       // let json = JSON(data: data) // moved this line
       let count: Int? = json.dictionary?.count
    
       let ct = 0
       if  ct < count {
         for index in 0...count!-1 {
           for category in json["category"].arrayValue {
            let categoryHeader = category["categoryHeader"].stringValue
            let section = category["sections"][0]["section"].stringValue
            let sectionBody = category["sections"][0]["body"].stringValue
            let obj = ["categoryHeader": categoryHeader, "section": section, "body": sectionBody] //, "body": body, "title": sigs]
    
            objects.append(obj)
          }
       }
        tableView.reloadData()
    }
    

    【讨论】:

    • json.array?.count的值是什么意思?它目前以 nil 的形式返回,因此永远不会执行以下语句。对不起,如果我错过了什么......
    • 查看编辑。它应该计算数组中的节数。
    • 啊,谢谢,我明白了。进行该修改后,let json = JSON(data:data) 出现“使用未解析的标识符‘数据’”错误。
    • 您如何检索 json 信息?查看帖子更新,看看是否有帮助。
    • 我更新了问题中的代码 sn-p 以显示我如何获取 json。应用最新的代码更改仍然存在“数据”问题。
    【解决方案2】:

    会:

    for section in category["sections"].arrayValue {
        let sectionName = section["section"].stringValue
        let body = section["body"].stringValue
    }
    

    工作?

    【讨论】:

    • 我已经更新了这个问题,并对我如何尝试实施这种方法进行了编辑。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-06
    • 1970-01-01
    • 2013-05-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多