ios - 无法将类型 'Swift.Array<Swift.Dictionary<Swift.String, Swift.String>>' () 的值转换为 'Swift.Dictionary<Swift.String, Any>' ()

标签 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")
}

当这样写时,我得到错误:Type 'Any' has no subscript members

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

当我这样写时错误:无法将类型“Swift.Array>”(0x10bf341f0)的值转换为“Swift.Dictionary”(0x10bf340d0)。/p>

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

我无法理解确切的问题是什么以及如何解决它。

最佳答案

要计算你需要做的...

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)")

关于ios - 无法将类型 'Swift.Array<Swift.Dictionary<Swift.String, Swift.String>>' () 的值转换为 'Swift.Dictionary<Swift.String, Any>' (),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50771627/

相关文章:

ios - 我如何检查 currentQueue 是主队列。?

javascript - AJAX - JSON 未定义(jQuery、NodeJS)

Java "Jackson"JsonMappingException : Can not deserialize instance of float out of FIELD_NAME token

ios - 泛型属性

swift - Segues 和 UIScrollView

ios - 如何通过 segue 将 NSTimeInterval 值从 swift 传递到 Objective C

iOS UITableView 单元格缩进

json - 从云函数获取随机wiki页面

ios - 为什么 UTTypeCreatePreferredIdentifierForTag 返回奇怪的 UTI?

ios - 在 Watch 应用程序使用的文件中有条件地使用 UIKit