-4
func parseResponseData(response: AnyObject) {
    if let feedData = response["feed"] as? [String: Any]{
        let entryArray = feedData["entry"] as Any
        if let entry = entryArray as? [Any]{
            for object in entry{
                print(object)
                let appName = object["category"] as [String: Any]  // Error: Type "Any" has no subscript members.
            }   
        } 
    }
}

I am not able to parse the response because of this error, any clue how to resolve this.

JSON response

Hamish
  • 78,605
  • 19
  • 187
  • 280

2 Answers2

0

try this, entryArray is an array of String : Any

func parseResponseData(response: AnyObject) {
    if let feedData = response["feed"] as? [String: Any]{
        let entryArray = feedData["entry"] as Any
        if let entry = entryArray as? [[String: Any]] {
            for object in entry {
                print(object)
                let appName = object["category"] as? [String: Any]
            }
        }
    }
}
JuicyFruit
  • 2,638
  • 2
  • 18
  • 35
0

You have to tell the compiler the actual types of all subscripted collection types, Any is not sufficient.

It's helpful to use a type alias for the JSON dictionary

typealias JSONDictionary = [String:Any]

func parseResponseData(response: Any) {
    if let feedData = response["feed"] as? JSONDictionary,
     let entryArray = feedData["entry"] as? [JSONDictionary] {
        for object in entryArray {
            print(object)
            if let category = object["category"] as? JSONDictionary {
                print(category)
            }
        } 
    }
}
vadian
  • 274,689
  • 30
  • 353
  • 361