0

I'm new to using Alamofire and have encountered an issue. I'm able to run the following code to print out all the data from an API endpoint.

Alamofire.request("http://codewithchris.com/code/afsample.json").responseJSON { response in
    if let JSON = response.result.value {
        print(JSON)
    }
}

The issue is that when I run this:

Alamofire.request("http://codewithchris.com/code/afsample.json").responseJSON { response in
    if let JSON = response.result.value {
        print(JSON["firstkey"])
    }
}

I get the error:

Type 'Any' has no subscript members

I don't know why this error is happening, it seems as if I'm accessing the data correctly. Any help would be great, thanks!

I have tried formatting it using both:

print(JSON["firstkey"] as String)

and

print(JSON["firstkey"] as [String:Any]

but they still give the same error.

This is the JSON on my endpoint:

{
    "firstkey":"it worked!",
    "secondkey":["item1", "item2", "item3"]
}
Joe Scotto
  • 10,936
  • 14
  • 66
  • 136
  • "Type 'Any' has no subscript members": Know error. Did you search for it? `JSON`/`response.result.value` is of type `Any`, it's not a Dictionary, so you can use subscript (which you do by doing `["firstKey"]` on it). You have to specify that i's a dictionary, by doing something like `as [String:Any]` – Larme Jun 04 '17 at 21:37
  • Possible duplicate of [Correctly Parsing JSON in Swift 3](https://stackoverflow.com/questions/39423367/correctly-parsing-json-in-swift-3) – Larme Jun 04 '17 at 21:38
  • @Larme I've searched for the error and have tried doing `print(JSON["firstkey"] as String)` and `print(JSON["firstkey"] as [String:Any]` both give the same error `Tyep 'Any' has no subscript members` – Joe Scotto Jun 04 '17 at 21:39
  • (I'll bypass the `print` ): `JSON["firstkey"] as [String:Any])` => It's saying: Consider `JSON["firstkey"]` as a Dictionary object (which keys are String), not Consider `JSON` as a Dictionary object (which keys are String). That's different. – Larme Jun 04 '17 at 21:41
  • @Larme I'm sorry, but that's not making much sense to me. I'm fairly new to Swift, I know most of the basics but making API calls using it is very new to me. Could you give me more information as to what I actually have to do to just be able to call `json["firstkey"]` and get the value? – Joe Scotto Jun 04 '17 at 21:43
  • try `if let JSON = response.result.value as? [String: Any]` – Maksym Musiienko Jun 04 '17 at 21:48

3 Answers3

1

This is really simple. You just need to force cast (as!) your JSON. so change your code to this and it will work:

Alamofire.request("http://codewithchris.com/code/afsample.json").responseJSON { response in
    if let JSON = response.result.value {
        let json = JSON as! [String: Any]
        print(json["firstkey"])
    }
}

Edit 1: As you said in comments that you are using SwiftyJSON package. Sample code is as follows:

Alamofire.request("http://codewithchris.com/code/afsample.json").responseJSON { response in
        if let value = response.result.value {
            let json = JSON(value)
            print(json["firstkey"].stringValue)
        }
    }

Alamofire.request("https://mmcalc.com/api").responseJSON { response in
        if let value = response.result.value {
            let json = JSON(value)
            print(json.arrayValue[0]["uniqueUsers"].stringValue)
        }
    }
  • That seems to be working but only partially. I'm now trying another [api](https://mmcalc.com/api) and I'm unable to access keys because of indexing. Is there a way I can just parse this and get a simple object where I can go `json[0]["key"]`? – Joe Scotto Jun 04 '17 at 22:27
  • I recommend you use [SwiftyJson](https://github.com/SwiftyJSON/SwiftyJSON) to parse your json into a JSON object. It makes your job way easier. If you do not use a json parser like SwiftyJSON, you have to find the correct format for each and every api that you consume. – Arash Tabrizian Jun 04 '17 at 22:30
  • I'm using SwiftyJson in my project, would it be simple to do this? Could you provide an example? – Joe Scotto Jun 04 '17 at 22:31
  • Thank you, I appreciate it very much! – Joe Scotto Jun 04 '17 at 22:39
  • You're welcome and do not forget to import SwiftyJSON at top of your file. – Arash Tabrizian Jun 04 '17 at 22:42
0

You are trying to get the value with getting the object, try this:

Alamofire.request("http://codewithchris.com/code/afsample.json").responseJSON { response in
if let result = response.result.value {
    let JSON = result as! NSDictionary
    print(JSON["firstkey"])
}
}

Hope it will work!

Zohaib Hassan
  • 984
  • 2
  • 7
  • 11
0

You should add ! at the end of code before ) to force unwrap the value

   Alamofire.request("http://codewithchris.com/code/afsample.json").responseJSON { response in
      if let JSON = response.result.value {
      let json = JSON as! [String: Any]
      print(json["firstkey"]!)
   }
}
Swym App
  • 1
  • 1
  • We can also use this code Alamofire.request("http://codewithchris.com/code/afsample.json").responseJSON { response in if let JSON = response.result.value { let json = JSON as! [String: Any] print(json["firstkey"] as Any) } } – Swym App Jun 28 '19 at 09:53