简体   繁体   中英

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

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

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

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM