通过Swift中的嵌入式JSON数组循环?

时间:2017-07-29 17:14:34

标签: ios arrays json swift

我试图遍历嵌入式JSON数组并提取所有值以放入本地数组。这就是JSON的样子:

"welcome": {
  "data": {
    "tncUrl": ""
  },
  "items": [
    {
      "newUser": [
        {
          "stepConcept": false
        },
        {
          "stepSafety": true
        },
        {
          "stepFacilitator": true
        },
        {
          "stepTransparency": true
        }
      ],
      "switcher": [
        {
          "stepConcept": true
        },
        {
          "stepSafety": true
        },
        {
          "stepFacilitator": true
        },
        {
          "stepTransparency": true
        }
      ]
    }
  ]
}

我能够达到一个可以看到我正在检索" newUser"的值的问题,问题是循环这些值并将它们添加到数组中。我这样做时收到了EXC_BAD_INSTRUCTION错误。这是我用来获取这些值的代码:

  func prepareArrayOfViews(userType: User)
    {
        if (welcomeJSON != nil)
        {
            let items : NSArray? = welcomeJSON!.value(forKey: "items") as? NSArray

            if (items == nil)
            {
                listOfViews = ["stepConcept", "stepSafety", "stepFacilitator", "stepTransparency"]
                maxPages = listOfViews.count
                return
            }

            if (items != nil) {

                if let newUser = (items?.value(forKey: "newUser") as? NSArray){

                    //Below is where the error "EXC_BAD_INSTRUCTION"
                    for key in (newUser as! NSDictionary).allKeys
                    {
                        if (((newUser as! NSDictionary).value(forKey: key as! String) as? Bool)!)
                        {
                            listOfViews.append(key as! String)
                        }
                    }

                }

                if (listOfViews.count == 0)
                {
                    listOfViews = ["stepConcept", "stepSafety", "stepFacilitator", "stepTransparency"]
                }

                maxPages = listOfViews.count
            }
        }
    }

2 个答案:

答案 0 :(得分:2)

我已将您的代码更改为使用本机Swift结构。由于当您的可选展开不起作用时,您没有处理错误或执行任何操作,因此我还将展开更改为保护语句。

除了Swift编码实践的严重问题之外,您的问题是您尝试将字典数组作为简单字典进行迭代。

func prepareArrayOfViews(userType: User){
    guard let welcomeJSON = welcomeJSON else {return}
    guard let items = welcomeJSON["items"] as? [[String:Any]] else {
        listOfViews = ["stepConcept", "stepSafety", "stepFacilitator", "stepTransparency"]
            maxPages = listOfViews.count
            return
    }
    for item in items {
        if let newUser = item["newUser"] as? [[String:Any]] {
            for embeddedDict in newUser {
                for (key, value) in embeddedDict { 
                    if let val = value as? Bool, val == true {
                        listOfViews.append(key)
                    }
                }
            }
        } else if let switcher = item["switcher"] as? [[String:Any]]{
            for embeddedDict in switcher {
                for (key, value) in embeddedDict { 
                    if let val = value as? Bool, val == true {
                        //do whatever you need to with the value
                    }
                }
            }
        }
    }
    if (listOfViews.count == 0){
         listOfViews = ["stepConcept", "stepSafety", "stepFacilitator", "stepTransparency"]
    }   
    maxPages = listOfViews.count
}

答案 1 :(得分:1)

因为

//here newUser is an NSArray

     if let newUser = (items?.value(forKey: "newUser") as? NSArray){

                        //here newUser forced to NSDictionary 
                        for key in (newUser as! NSDictionary).allKeys

尝试将此部分更改为

if let newUsers = (items?.value(forKey: "newUser") as? NSArray){

                for newUser in newUsers
                {
                    for key in (newUser as! NSDictionary).allKeys
                {
                    if (((newUser as! NSDictionary).value(forKey: key as! String) as? Bool)!)
                    {
                        listOfViews.append(key as! String)
                    }
                }
                }

            }