无法转换类型'Swift.Array <swift.dictionary <swift.string,swift.string =“”>&gt;'的值()到'Swift.Dictionary <swift.string,any =“”>'()

时间:2018-06-09 06:38:49

标签: ios json swift post nsdictionary

这是我的JSON,当我阅读ord时,uniq数据我收到错误

let response2 : [String: Any]  = ["Response":["status":"SUCCESS","error_code":"0","message":"SUCCESS","Array":[
                                ["ord":"50","uniq":"5a66c2348",
                                   "name":"SHREE","no":"UP11AT0","loc":"Hathin","lat":"23.33","lon":"87.09","date":"30-01-2018","time":"12:35:33","dis_dt":"00-00-0000","dis_tm":"00:00:00"],
                                ["ord":"50","uniq":"5a66c2348",
                                   "name":"SHREE","no":"UP11AT0","loc":"Hathin","lat":"23.33","lon":"87.09","date":"30-01-2018","time":"12:35:33","dis_dt":"00-00-0000","dis_tm":"00:00:00"],
                                ["ord":"50","uniq":"5a66c2348",
                                   "name":"SHREE","no":"UP11AT0","loc":"Hathin","lat":"23.33","lon":"87.09","date":"30-01-2018","time":"12:35:33","dis_dt":"00-00-0000","dis_tm":"00:00:00"]
                            ]],"count":"35"]

我的代码是

let status = "\((response2["Response"]! as! [String: Any])["status"]!)"
print(status)

if status == "SUCCESS" {
   let count = ((response2["Response"]! as! [String: Any])["Array"] as AnyObject).count!
   print(count)
   let ar = (response2["Response"]! as! [String: Any])["Array"]!
   print(ar)

   var ord_id: [Any] = []
   for i in 0..<count {
       ord_id.append((response2["Response"]! as! [String: Any])["Array"]! [0] as! [String:Any])// Here I'm getting Type 'Any' has no subscript members and 
   }

} else {
   print("Show alert")
}

写这样的时候我收到错误: 类型'任何'没有下标成员

let ar = (response2["Response"]! as! [String: Any])["Array"]! [0] as! [String: Any]          

当我写这样的错误时: 无法转换类型'Swift.Array&gt;'的值(0x10bf341f0)到'Swift.Dictionary'(0x10bf340d0)。

let ar = (response2["Response"]! as! [String: Any])["Array"]! as! [String: Any]

我无法理解如何解决它的确切问题。

2 个答案:

答案 0 :(得分:1)

要计算,你需要做...

if let res = response2["Response"] as? [String: Any], let arr = res["Array"] as? [[String: Any]] {
  print("array count = \(arr.count)")
} else {
   print("Array not found !!")
}

从“响应”键获取数组...

var ord_id: [Any] = []

if let res = response2["Response"] as? [String: Any] {
    if let arr = res["Array"] as? [Any], arr.count > 0 {
      print(arr)
      ord_id = arr
    }
}

修改

要获得“old”和“uniq”键值,您需要通过一个数组并获取所需的对象。

var ordId_arr = [String]()
var uniq_arr = [String]()

for obj in ord_id {

 print(obj)

 if let dict = obj as? [String: Any] {
    //print(dict["ord"] as! String)
    //print(dict["uniq"] as! String)
    //print(dict["name"] as! String)
   //you can get other values in same way

   if let ord = dict["ord"] as? String {
        ordId_arr.append(ord)
    }

    if let uniq = dict["uniq"] as? String {
        uniq_arr.append(uniq)
    }
 }

}

print("\(ordId_arr)")
print("\(uniq_arr)")

答案 1 :(得分:1)

Parse可以通过,也可以使用Decodable将JSON解析为对象。

let response = response2["Response"]! as! [String: Any]
let status = response["status"]!

if let array = response["Array"] as? Array<Dictionary<String, Any>> {
     print(array.count)   
     let ordArray = array.map { $0["ord"] }
     let unique = Array(Set(ordArray)) //Do if needs to stripe duplicates
     let uniqArray = array.map { $0["uniq"] }
     print(ordArray)
     print(uniqArray)
}