Hi my question is related to json object. i have this link \"http://ip-api.com/json\" and this link gives the details of your IP Address. i only need to print IP Address from th
The IP address is the value for key query
on the top level of the JSON
let json = try JSONSerialization.jsonObject(with: data!, options:.allowFragments) as! [String:Any]
if let query = json["query"] as? String {
print(query)
}
In Swift 3 the type of a JSON dictionary is [String:Any]
PS: You don't need a URL request for this task, pass the URL directly and use the native structs URL
(and URLRequest
)
let requestURL = URL(string: "http://ip-api.com/json")!
...
let task = session.dataTask(with: requestURL) {