Json解析Swift没有得到打印
Posted
技术标签:
【中文标题】Json解析Swift没有得到打印【英文标题】:Json parsing Swift not getting print 【发布时间】:2020-01-28 07:05:39 【问题描述】:我是 Swift 的新手。我想使用 url 从服务器获取一些 json 数据。我尝试了许多其他解决方案,但没有奏效。我想从数组中打印duration
键(文本和值),然后在控制台中打印。
Json 数据附在下方
"status": "OK",
"rows": [
"elements": [
"duration":
"text": "3 hours 49 mins",
"value": 13725
,
"distance":
"text": "225 mi",
"value": 361715
,
"status": "OK"
]
],
"origin_addresses": [
"Washington, DC, USA"
],
"destination_addresses": [
"New York, NY, USA"
]
附加代码
func getdatajson1()
if let url = URL(string: "http://www.json-generator.com/api/json/get/bQywstyfkO?indent=2")
URLSession.shared.dataTask(with: url) data, response, error in
if let data = data
do
let res = try JSONDecoder().decode(Root.self, from: data)
print(res.rows)
catch let error
print(error)
.resume()
struct Root: Codable
let rows: [Root2]
struct Root2: Codable
let elements: [Root3]
struct Root3: Codable
let elements: [node]
struct node: Codable
let duration : [valuesarray]
struct valuesarray: Codable
let text : String
let value : Int
【问题讨论】:
什么是打印res.rows
这个语句?
什么都没有!!它的代码错误
是打印nil
还是别的什么?
找不到它的打印密钥
“未找到其打印密钥”。然后它正在打印`print(error)`,但是请阅读该错误,或者如果您不理解它,请将其添加到您的问题中,它包含有关问题的重要信息。
【参考方案1】:
duration
是 Object
而不是 Array
,也可以更改您的名称,您可以使用它:
struct Root: Decodable
let rows: [Rows]
struct Rows: Decodable
let elements: [Elements]
struct Elements: Decodable
let duration, distance: LocationValues
struct LocationValues: Decodable
let text: String
let value: Int
func getdatajson1()
if let url = URL(string: "http://www.json-generator.com/api/json/get/bQywstyfkO?indent=2")
URLSession.shared.dataTask(with: url) data, response, error in
if let data = data
do
let res = try JSONDecoder().decode(Root.self, from: data)
if let row = res.rows.first, let elements = row.elements.first
print(elements.duration.text) //This is how you can get the text value
print(elements.distance.text) //This will print the distance
catch let error
print(error)
.resume()
【讨论】:
``` 持续时间是[Time_String.jsonViewController.Rows(elements: [Time_String.jsonViewController.Elements(duration: Time_String.jsonViewController.LocationValues(text: "3 hours 49 mins", value: 13725) , distance: Time_String.jsonViewController.LocationValues(text: "225 mi", value: 361715))])] ``` 它打印这个但不是持续时间文本的确切值 要获取duration
的值,您必须从中获取值。让我更新我的答案。
确定!!更新它
@DevenNazare 更新。
谢谢它的工作!你能把距离和持续时间都打印出来吗?将不胜感激【参考方案2】:
用下面的替换你的可编码结构
class Result: Codable
var status:String?
var rows:[Row]?
var origin_addresses:[String]?
var destination_addresses:[String]?
class Row: Codable
var elements:[Element]?
class Element: Codable
var status:String?
var duration:Duration?
var distance:Distance?
class Duration: Codable
var text:String?
var value:Int?
class Distance: Codable
var text:String?
var value:Int?
【讨论】:
【参考方案3】:您应该更新您的node
模型,如下所示
struct node: Codable
let duration : valuesarray
let distance : valuesarray
let status : String
您可以从 API 响应中访问您的 duration
数据,如下所示
if let rows = res.rows, rows.count > 0
//Access the element objects from rows
let arrElements = rows[0].elements, arrElements.count > 0
if let durationData = arrElements[0].duration //get your duration object
print(durationData.text)
print(durationData.value)
【讨论】:
【参考方案4】:试试这个:
和一个提示:转到https://app.quicktype.io/ -> 在这里您可以粘贴您的 json,您将免费获得您的数据结构! ;)
func getdatajson1()
if let url = URL(string: "http://www.json-generator.com/api/json/get/bQywstyfkO?indent=2")
URLSession.shared.dataTask(with: url) data, response, error in
if let data = data
do
let res = try JSONDecoder().decode(Welcome.self, from: data)
print(res.rows)
catch let error
print(error)
.resume()
getdatajson1()
struct Welcome: Codable
let status: String
let rows: [Row]
let originAddresses, destinationAddresses: [String]
enum CodingKeys: String, CodingKey
case status, rows
case originAddresses = "origin_addresses"
case destinationAddresses = "destination_addresses"
// MARK: - Row
struct Row: Codable
let elements: [Element]
// MARK: - Element
struct Element: Codable
let duration, distance: Distance
let status: String
// MARK: - Distance
struct Distance: Codable
let text: String
let value: Int
【讨论】:
以上是关于Json解析Swift没有得到打印的主要内容,如果未能解决你的问题,请参考以下文章