0
您好,我的问题与json对象有关。我有这个链接“http://ip-api.com/json”,这个链接给出了你的IP地址的详细信息。我只需要在swift 3中打印来自这个json文件的IP地址。我很新可能是我的问题很基本,但我需要一些帮助来整理我的项目。所以我做了如下。如何快速解析Json对象3
let requestURL: NSURL = NSURL(string: "http://ip-api.com/json")!
let urlRequest: NSMutableURLRequest = NSMutableURLRequest(url: requestURL as URL)
let session = URLSession.shared
let task = session.dataTask(with: urlRequest as URLRequest) {
(data, response, error) -> Void in
let httpResponse = response as! HTTPURLResponse
let statusCode = httpResponse.statusCode
if (statusCode == 200) {
print("Everyone is fine, file downloaded successfully.")
do{
let json = try JSONSerialization.jsonObject(with: data!, options:.allowFragments) as! [String:AnyObject]
if let stations = json["city"] as? [[String: AnyObject]] {
for station in stations {
if let name = station["regionName"] as? String {
self.names.append(name)
print("this is query\(name)")
}
else{
print ("no ip address is found")
}
}
}
}
catch {
print("Error with Json: \(error)")
}
}
}
task.resume()
非常感谢提前。
非常感谢在干活很棒! –
然后请接受答案,点击复选标记。 (OT:我住在Altnau附近) – vadian
谢谢我接受了你的回答。 –