0

我正在尝试访问 json 文件中的数据,但不完全确定如何访问。我正在尝试访问此 json 中的“animation_file”:

 "items": [{
                    "newUser": {
                    "steps": [
                              {
                              "id": 0,
                              "step_title": "stepConcept",
                              "visible": false,
                              "animation_file": "Welcome_Step_01_V01"
                              },
                              {
                              "id": 1,
                              "step_title": "stepSafety",
                              "visible": true,
                              "animation_file": "Welcome_Step_02_V01"
                              },
                              {
                              "id": 2,
                              "step_title": "stepFacilitator",
                              "visible": true,
                              "animation_file": "Welcome_Step_03_V01"
                              },
                              {
                              "id": 3,
                              "step_title": "stepTransparency",
                              "visible": true,
                              "animation_file": "Welcome_Step_04_V01"
                              }
                              ]
                    },

这是我到目前为止所拥有的:

 guard let items = welcomeJSON["items"] as? [[String:Any]] else {return}

        for item in items {
            if (isNewUser) {
                if let newUser = item["newUser"] as? [String:Any] {
                    if let steps = newUser["steps"] as? [[String:Any]] {
                        for embeddedDict in steps {
                            for (key, value) in embeddedDict {
                                if let val = value as? Bool, val == true {
                                    print(key)
                                    newUserViews.append(key)
                                }
                            }
                        }
                    } else {
                        listOfViews = newUserViews
                        listOfViews = [STEP_CONCEPT, STEP_SAFETY, STEP_FACILITATOR, STEP_TRANSPARENCY]
                        maxPages = listOfViews.count
                        return
                    }
                }
            }

这看起来正确吗?我觉得我可能错过了一步。

4

1 回答 1

0

如果我理解正确,只有当 visible 的值为 true 并且您想将其放入 newUserViews 数组时,您才试图获取“animation_file”的值。下面的代码应该可以帮助您:

guard let items = welcomeJSON["items"] as? [[String:Any]] else {return}

    for item in items {
        if (isNewUser) {
            if let newUser = item["newUser"] as? [String:Any] {
                if let steps = newUser["steps"] as? [[String:Any]] {
                    for embeddedDict in steps {
                        if let visible = newUser["visible"] as? Bool, visible == true, let animationFile = newUser["animation_file"] as? String  {
                            newUserViews.append(animationFile)
                        }
                    }
                } else {
                    listOfViews = newUserViews
                    listOfViews = [STEP_CONCEPT, STEP_SAFETY, STEP_FACILITATOR, STEP_TRANSPARENCY]
                    maxPages = listOfViews.count
                    return
                }
            }
        }
    }

在我看来,您的其余代码似乎不正确,但上面的代码应该可以解决您当前的问题。

于 2017-10-25T15:42:56.503 回答