本文介绍了如何从Swift 3.0中返回的json获取特定值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试从Swift中的json获取值.我添加了数据树的图像.我以前的尝试没有用.下面的代码打印了我不想要的完整的json对象.
I am trying to get a value from json in Swift. I have added an image of the data tree. My previous attempts have not worked. Below is code which prints the full json object which is what I don't want.
json树图像
import PlaygroundSupport import UIKit let url = URL(string: "api.data.gov.sg/v1/transport/taxi-availability") var request = URLRequest(url: url!); request.addValue("xxxx", forHTTPHeaderField: "api-key") let task = URLSession.shared.dataTask(with: request) { data, response, error in guard error == nil else { print(error) return } guard let data = data else { print("Data is empty") return } let json = try! JSONSerialization.jsonObject(with: data, options: []) //print(json) }//end //["features"]??[0]? task.resume() PlaygroundPage.current.needsIndefiniteExecution = true推荐答案
您只需要对已经售出的json做些事情:
You just need to do something with the json you've been vended:
let task = URLSession ... { data, response, error in let json = JSONSerialization.jsonObject(...) if let json = json as? [String: Any] { // now you have a top-level json dictionary for key, value in json { print("json[\"\(key\")] = \(value)") } } }更多推荐
如何从Swift 3.0中返回的json获取特定值?
发布评论