Q:

Reading JSON output in Swift

I am trying to read a JSON output from a web app. This output is:
[{"group_name":"XYZ","adminof":0}]

I have a struct that looks like:
struct grouplistStruct{
var group_name : String
var adminof : Any
}

The code that I am using is:
let jsonArray = try JSONSerialization.jsonObject(with: data, options: []) as! [Any]
for jsonResult in jsonArray{

          let loc = grouplistStruct(group_name: jsonResult["group_name"], adminof: jsonResult["adminof"])

I can see that jsonArray reads the value correctly. Similarly in the for loop, jsonResult is also reading the value correctly
But when I try to assign this value to a Struct variable, it shows an error:
Type 'Any' has no subscript members

Why is that happening? Sorry I am new to Swift so I am learning all this.

A:

Since your json data is an array containing dictionaries like this:
[{"group_name":"XYZ","adminof":0}]

You are getting the error 

Type 'Any' has no subscript members

because you are downcasting the json as an array of Any  and in swift Any (as the name suggests) represents any type like Int, Double or Dictionary of type [String: String] as you know that Int or Double can not have subscript like someInt["subscript"]
So you need to downcast to this specific type using [[String: Any]] .
This represents an array containing the dictionaries of type [String: Any]. This will work because dictionaries can have subscript members e.g someDict["group_name"]
Hence you should use [[String: Any]] instead of [Any] in this statement try JSONSerialization.jsonObject(with: data, options: []) as! [Any]

