简体   繁体   English

无法转换类型为&#39;Swift.Array的值 <Swift.Dictionary<Swift.String, Swift.String> &gt;”()到“ Swift.Dictionary” <Swift.String, Any> &#39;()

[英]Could not cast value of type 'Swift.Array<Swift.Dictionary<Swift.String, Swift.String>>' () to 'Swift.Dictionary<Swift.String, Any>' ()

This is my JSON, when i read ord , uniq data i'm getting errors 这是我的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"]

My code is 我的代码是

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

When write like this I'm getting Error: Type 'Any' has no subscript members 当这样写时,我得到错误: 类型“ Any”没有下标成员

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

When I write like this Error: Could not cast value of type 'Swift.Array>' (0x10bf341f0) to 'Swift.Dictionary' (0x10bf340d0). 当我这样写时:错误: 无法将类型'Swift.Array>'(0x10bf341f0)的值强制转换为'Swift.Dictionary'(0x10bf340d0)。

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

I'm unable to understand what is the exact problem an how to solve it. 我无法理解确切的问题是什么以及如何解决。

To get count you need to do... 要计数,您需要做...

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

To get array from "Response" key... 要从“响应”键获取数组...

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
    }
}

EDIT: 编辑:

To get "old" and "uniq" key value, you need to get through an array and get required object. 要获取“ 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)")

Parse can be done by, Also you can use Decodable for parsing JSON into objects. 可以通过进行解析,也可以使用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)
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法转换类型“Swift.Array”的值<Any> &#39; 到 &#39;Swift.Dictionary<Swift.String, Any> &#39; - Could not cast value of type 'Swift.Array<Any>' to 'Swift.Dictionary<Swift.String, Any>' 类型不匹配(Swift.Dictionary<swift.string, any> ,</swift.string,> - typeMismatch(Swift.Dictionary<Swift.String, Any>, 类型不匹配(Swift.Dictionary<Swift.String, Any> debugDescription: &quot;预期解码字典<String, Any>而是找到了一个数组。” - typeMismatch(Swift.Dictionary<Swift.String, Any> debugDescription: "Expected to decode Dictionary<String, Any> but found an array instead." 无法转换类型为&#39;Swift._SwiftDeferredNSDictionary&#39;的值 <Swift.String, Swift.String> ”到“ NSMutableDictionary” - Could not cast value of type 'Swift._SwiftDeferredNSDictionary<Swift.String, Swift.String>' to 'NSMutableDictionary' 无法将“SwiftyJSON.JSON”类型的值转换为“Swift.String” - Could not cast value of type 'SwiftyJSON.JSON' to 'Swift.String' “unsafeMutableAddressor : Swift.String”,引用自:...” - "unsafeMutableAddressor : Swift.String", referenced from:.." 无法转换类型为&#39;Swift._NativeDictionaryStorageOwner的值 <Swift.String, Swift.AnyObject> &#39;(0x40c2134)到&#39;NSArray&#39;(0x383bb8d8) - Could not cast value of type 'Swift._NativeDictionaryStorageOwner<Swift.String, Swift.AnyObject>' (0x40c2134) to 'NSArray' (0x383bb8d8) 存档iOS App时出错-“通用专业化 <Swift.String> Swift.String.init” - Error when archiving iOS App - “generic specialization <Swift.String> of Swift.String.init” 在Swift中将任何字典数组转换为String - Convert Any dictionary array to String in Swift Swift Codable:无法解码 [String: Any] 或 [String: Decodable] 类型的字典 - Swift Codable: Cannot decode dictionary of type [String: Any] or [String: Decodable]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM