如何从Swift 3.0中返回的json获取特定值?

编程入门 行业动态 更新时间:2024-10-06 14:38:36
本文介绍了如何从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获取特定值?

本文发布于:2023-11-26 21:44:59,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1635310.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:定值   Swift   json

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!