JSON 在 swift 中响应解码为 nil
JSON response decoding to nil in swift
struct Jresponse: Decodable{
//var results: [Any]
var resolution: Int?
var elevation: Int?
var location: Int?
var lat: Int?
var lng: Int?
}
guard let url = URL(string: "https://maps.googleapis.com/maps/api/elevation/json?&locations=\(finalStringConvertedCoordinates)&key=\(apiKey)") else {
print("Error: cannot create URL")
return
}
// Create the url request
var request = URLRequest(url: url)
request.httpMethod = "GET"
URLSession.shared.dataTask(with: request) { data, response, error in
guard error == nil else {
print("Error: error calling GET")
print(error!)
return
}
guard let data = data else {
print("Error: Did not receive data")
return
}
guard let response = response as? HTTPURLResponse, (200 ..< 299) ~= response.statusCode else {
print("Error: HTTP request failed")
return
}
do {
guard let jsonObject = try JSONSerialization.jsonObject(with: data) as? [String: Any] else {
print("Error: Cannot convert data to JSON object")
return
}
guard let prettyJsonData = try? JSONSerialization.data(withJSONObject: jsonObject, options: []) else {
print("Error: Cannot convert JSON object to Pretty JSON data")
return
}
guard let prettyPrintedJson = String(data: prettyJsonData, encoding: .utf8)
else {
print("Error: Could print JSON in String")
return
}
//print(prettyPrintedJson)
jsonResponse = prettyPrintedJson
} catch {
print("Error: Trying to convert JSON data to string")
return
}
let decoder = JSONDecoder()
let jrData = jsonResponse.data(using: .utf8)
let jsonResponseProcessed = try! decoder.decode(Jresponse.self, from: jrData! )
print(jsonResponseProcessed)
}.resume()
}
上面的代码包含请求和响应的解码,我对 JSON 没有经验,这可能很明显。
我遇到的问题是打印时返回的结果都是零。
以前我没有使用解码,而是试图将数据处理成一个数组,在该数组中可以检索各个值以进行比较。
数组方法生成难以管理的数据,因为元素未按可管理的顺序分隔。
像这样[无响应][1]
包含 print("---> data: (String(data: data, encoding: .utf8))") 的输出看起来像这样。
[输出可以在这里看到][2]
---> data: Optional("{\n "results" : [\n {\n "elevation" : 11.59268283843994,\n "location" : {\n "lat" : 37.78533554077148,\n "lng" : -122.4059982299805\n },\n "resolution" : 0.5964969992637634\n },\n {\n "elevation" : 11.68190288543701,\n "location" : {\n " lat" : 37.78538513183594,\n "lng" : -122.4059143066406\n },\n "resolution" : 0.5964969992637634\n },\n {\n "elevation" : 11.86490249633789,\n " location" : {\n "lat" : 37.78548812866211,\n "lng" : -122.4059143066406\n },\n "resolution" : 0.5964969992637634\n },\n {\n "海拔" : 11.53255939483643,\n "location" : {\n "lat" : 37.78533554077148,\n "lng" : -122.4061584472656\n },\n "resolution" : 0.5964969992637634\n }, \n {\n “海拔”:11.46291351318359,\n “位置”:{\n “纬度”:37.78533554077148,\n “经度”:-12 2.4063262939453\n },\n "resolution": 0.5964969992637634\n },\n {\n "elevation": 11.69645023345947,\n "location": {\n "lat": 37.78558731079102, \n“lng”:-122.4059143066406\n},\n“分辨率”:0.5964969992637634\n},\n{\n“海拔”:11.79109859466553,\n“位置”:{\ n "lat" : 37.78569030761719,\n "lng" : -122.4059143066406\n },\n "resolution" : 0.5964969992637634\n },\n {\n "elevation" : 11.357332222961426,\ n "location" : {\n "lat" : 37.78533554077148,\n "lng" : -122.4064865112305\n },\n "resolution" : 0.5964969992637634\n },\n {\n “海拔”:11.57364559173584,\n “位置”:{\n “纬度”:37.78533554077148,\n “经度”:-122.4066467285156\n },\n “分辨率”:0.5964969992637634\n },\n {\n “海拔”:11.7429027557373,\n “位置”:{\n “纬度”:37.78578948974609,\n "lng" : -122.4059143066406\n },\n "resolution" : 0.5964969992637634\n },\n {\n "elevation" : 12.02133083343506,\n "location" : {\n " lat" : 37.78589248657227,\n "lng" : -122.4059143066406\n },\n "resolution" : 0.5964969992637634\n },\n {\n "elevation" : 11.66207313537598,\n " location" : {\n "lat" : 37.78533554077148,\n "lng" : -122.4068145751953\n },\n "resolution" : 0.5964969992637634\n },\n {\n "海拔" : 11.85497093200684,\n "location" : {\n "lat" : 37.78536987304688,\n "lng" : -122.4069137573242\n },\n "resolution" : 0.5964969992637634\n }, \n {\n “海拔”:12.22011947631836,\n “位置”:{\n “纬度”:37.78599548339844,\n “经度”:-122.4059143066406\n },\n “分辨率”:0。 5964969992637634\n },\n {\n "海拔": 12.59670543670654,\n "位置": {\n "纬度": 37.78609466552 734,\n "lng": -122.4059143066406\n },\n "分辨率": 0.5964969992637634\n },\n {\n "海拔": 12.19275951385498,\n "位置": {\n "lat" : 37.78546905517578,\n "lng" : -122.4069137573242\n },\n "resolution" : 0.5964969992637634\n },\n {\n "elevation" : 12.13292598724365 ,\n "location" : {\n "lat" : 37.78557205200195,\n "lng" : -122.4069137573242\n },\n "resolution" : 0.5964969992637634\n },\n { \n“海拔”:12.98763847351074,\n“位置”:{\n“lat”:37.78619766235352,\n“lng”:-122.4059143066406\n},\n“分辨率”:0.5964969992637634\n },\n {\n "海拔": 13.3654317855835,\n "位置": {\n "纬度": 37.78629684448242,\n "经度": -122.4059143066406\n },\n "分辨率" : 0.5964969992637634\n },\n {\n "海拔": 12.14237785339355,\n "位置": {\n "lat" : 37.78567504882812,\n "lng": -122.4069137573242\n },\n "分辨率": 0.5964969992637634\n },\n {\n "海拔": 12.26948738098145,\n "位置": {\n "lat" : 37.78577423095703,\n "lng" : -122.4069137573242\n },\n "resolution" : 0.5964969992637634\n },\n {\n "elevation" : 13.74397277832031 ,\n "location" : {\n "lat" : 37.78633499145508,\n "lng" : -122.4060211181641\n },\n "resolution" : 0.5964969992637634\n },\n { \n“海拔”:14.1032075881958,\n“位置”:{\n“纬度”:37.78633499145508,\n“经度”:-122.4061889648438\n},\n“分辨率”:0.5964969992637634\n },\n {\n "海拔": 12.31325626373291,\n "位置": {\n "纬度": 37.7858772277832,\n "经度": -122.4069137573242\n },\n "分辨率" : 0.5964969992637634\n },\n {\n "海拔": 12.36202812194824,\n "位置": {\n "lat" : 37.78598022460938,\n "lng" : -122.4069137573242\n },\n "resolution" : 0.5964969992637634\n },\n {\n "elevation" : 14.12999248504639,\n "location" : {\n "lat" : 37.78633499145508,\n "lng" : -122.4063491821289\n },\n "resolution" : 0.5964969992637634\n },\n {\n "高程”:14.4321174621582,\n “位置”:{\n “lat”:37.78633499145508,\n “lng”:-122.4065093994141\n },\n “分辨率”:0.5964969992637634\n } ,\n {\n "海拔": 12.53042221069336,\n "位置": {\n "纬度": 37.78607940673828,\n "经度": -122.4069137573242\n },\n "分辨率": 0 .5964969992637634\n },\n {\n "海拔": 13.14097881317139,\n "位置": {\n "纬度": 37.78618240356445,\n "经度": -122.4069137573242\n }, \n "resolution" : 0.5964969992637634\n },\n {\n "elevation" : 14.39413261413574,\n "location " : {\n "lat" : 37.78633499145508,\n "lng" : -122.4066696166992\n },\n "分辨率" : 0.5964969992637634\n },\n {\n "海拔" : 14.40865802764893,\n "location" : {\n "lat" : 37.78633499145508,\n "lng" : -122.4068374633789\n },\n "resolution" : 0.5964969992637634\n },\ n {\n “海拔”:13.98401737213135,\n “位置”:{\n “纬度”:37.78628158569336,\n “经度”:-122.4069137573242\n },\n “分辨率”:0.5964969992637634\n }\n ],\n "状态" : "OK"\n}\n")
Jresponse(分辨率:无,高程:无,位置:无,纬度:无,经度:无)
[1]: https://i.stack.imgur.com/uaRZZ.png
[2]: https://i.stack.imgur.com/MWJZX.png
根据@Joakim Danielson 的建议,您应该将代码修改为
let jsonResponseProcessed = try! decoder.decode(Jresponse.self, from: data )
根据 google 文档,位于:https://developers.google.com/maps/documentation/elevation/start
你应该使用这个数据结构:EDIT
struct Jresponse: Codable {
let results: [Result]
let status: String?
}
struct Result: Codable {
let elevation: Double
let location: Location
let resolution: Double?
}
struct Location: Codable {
let lat, lng: Double
}
解码失败
您无法获取任何数据的最可能原因是您的数据模型(此处定义为结构 Jresponse
)不符合后端发回的实际 JSON 格式。
我已经快速检查了 google 地图 api 文档的海拔高度,您似乎应该收到一个 json 对象,其中包含顶层的 ElevationResult 数组。然后每个结果将包含分辨率、海拔和位置。
请参考https://developers.google.com/maps/documentation/elevation/overview#ElevationResult
正确的数据模型
struct ElevationResponse: Decodable {
let results: [ElevationResult]
}
struct ElevationResult: Decodable {
let elevation: Double
let location: LatLngLiteral
let resolution: Double?
}
struct LatLngLiteral: Decodable {
let lat: Double
let lng: Double
}
更改解码中的模型
let jsonResponseProcessed = try! decoder.decode(ElevationResponse.self, from: data)
边注
- 解码数据的方式可以更现代化和更清洁
- 注意不要使用force unwrap和force try
- 发布您的问题时,请注意代码缩进,首先 select 您在 Xcode 中的代码,然后执行
ctrl + i
以提高其可读性。然后才将其复制到您的 Whosebug 问题中。
struct Jresponse: Decodable{
//var results: [Any]
var resolution: Int?
var elevation: Int?
var location: Int?
var lat: Int?
var lng: Int?
}
guard let url = URL(string: "https://maps.googleapis.com/maps/api/elevation/json?&locations=\(finalStringConvertedCoordinates)&key=\(apiKey)") else {
print("Error: cannot create URL")
return
}
// Create the url request
var request = URLRequest(url: url)
request.httpMethod = "GET"
URLSession.shared.dataTask(with: request) { data, response, error in
guard error == nil else {
print("Error: error calling GET")
print(error!)
return
}
guard let data = data else {
print("Error: Did not receive data")
return
}
guard let response = response as? HTTPURLResponse, (200 ..< 299) ~= response.statusCode else {
print("Error: HTTP request failed")
return
}
do {
guard let jsonObject = try JSONSerialization.jsonObject(with: data) as? [String: Any] else {
print("Error: Cannot convert data to JSON object")
return
}
guard let prettyJsonData = try? JSONSerialization.data(withJSONObject: jsonObject, options: []) else {
print("Error: Cannot convert JSON object to Pretty JSON data")
return
}
guard let prettyPrintedJson = String(data: prettyJsonData, encoding: .utf8)
else {
print("Error: Could print JSON in String")
return
}
//print(prettyPrintedJson)
jsonResponse = prettyPrintedJson
} catch {
print("Error: Trying to convert JSON data to string")
return
}
let decoder = JSONDecoder()
let jrData = jsonResponse.data(using: .utf8)
let jsonResponseProcessed = try! decoder.decode(Jresponse.self, from: jrData! )
print(jsonResponseProcessed)
}.resume()
}
上面的代码包含请求和响应的解码,我对 JSON 没有经验,这可能很明显。
我遇到的问题是打印时返回的结果都是零。
以前我没有使用解码,而是试图将数据处理成一个数组,在该数组中可以检索各个值以进行比较。
数组方法生成难以管理的数据,因为元素未按可管理的顺序分隔。
像这样[无响应][1]
包含 print("---> data: (String(data: data, encoding: .utf8))") 的输出看起来像这样。
[输出可以在这里看到][2]
---> data: Optional("{\n "results" : [\n {\n "elevation" : 11.59268283843994,\n "location" : {\n "lat" : 37.78533554077148,\n "lng" : -122.4059982299805\n },\n "resolution" : 0.5964969992637634\n },\n {\n "elevation" : 11.68190288543701,\n "location" : {\n " lat" : 37.78538513183594,\n "lng" : -122.4059143066406\n },\n "resolution" : 0.5964969992637634\n },\n {\n "elevation" : 11.86490249633789,\n " location" : {\n "lat" : 37.78548812866211,\n "lng" : -122.4059143066406\n },\n "resolution" : 0.5964969992637634\n },\n {\n "海拔" : 11.53255939483643,\n "location" : {\n "lat" : 37.78533554077148,\n "lng" : -122.4061584472656\n },\n "resolution" : 0.5964969992637634\n }, \n {\n “海拔”:11.46291351318359,\n “位置”:{\n “纬度”:37.78533554077148,\n “经度”:-12 2.4063262939453\n },\n "resolution": 0.5964969992637634\n },\n {\n "elevation": 11.69645023345947,\n "location": {\n "lat": 37.78558731079102, \n“lng”:-122.4059143066406\n},\n“分辨率”:0.5964969992637634\n},\n{\n“海拔”:11.79109859466553,\n“位置”:{\ n "lat" : 37.78569030761719,\n "lng" : -122.4059143066406\n },\n "resolution" : 0.5964969992637634\n },\n {\n "elevation" : 11.357332222961426,\ n "location" : {\n "lat" : 37.78533554077148,\n "lng" : -122.4064865112305\n },\n "resolution" : 0.5964969992637634\n },\n {\n “海拔”:11.57364559173584,\n “位置”:{\n “纬度”:37.78533554077148,\n “经度”:-122.4066467285156\n },\n “分辨率”:0.5964969992637634\n },\n {\n “海拔”:11.7429027557373,\n “位置”:{\n “纬度”:37.78578948974609,\n "lng" : -122.4059143066406\n },\n "resolution" : 0.5964969992637634\n },\n {\n "elevation" : 12.02133083343506,\n "location" : {\n " lat" : 37.78589248657227,\n "lng" : -122.4059143066406\n },\n "resolution" : 0.5964969992637634\n },\n {\n "elevation" : 11.66207313537598,\n " location" : {\n "lat" : 37.78533554077148,\n "lng" : -122.4068145751953\n },\n "resolution" : 0.5964969992637634\n },\n {\n "海拔" : 11.85497093200684,\n "location" : {\n "lat" : 37.78536987304688,\n "lng" : -122.4069137573242\n },\n "resolution" : 0.5964969992637634\n }, \n {\n “海拔”:12.22011947631836,\n “位置”:{\n “纬度”:37.78599548339844,\n “经度”:-122.4059143066406\n },\n “分辨率”:0。 5964969992637634\n },\n {\n "海拔": 12.59670543670654,\n "位置": {\n "纬度": 37.78609466552 734,\n "lng": -122.4059143066406\n },\n "分辨率": 0.5964969992637634\n },\n {\n "海拔": 12.19275951385498,\n "位置": {\n "lat" : 37.78546905517578,\n "lng" : -122.4069137573242\n },\n "resolution" : 0.5964969992637634\n },\n {\n "elevation" : 12.13292598724365 ,\n "location" : {\n "lat" : 37.78557205200195,\n "lng" : -122.4069137573242\n },\n "resolution" : 0.5964969992637634\n },\n { \n“海拔”:12.98763847351074,\n“位置”:{\n“lat”:37.78619766235352,\n“lng”:-122.4059143066406\n},\n“分辨率”:0.5964969992637634\n },\n {\n "海拔": 13.3654317855835,\n "位置": {\n "纬度": 37.78629684448242,\n "经度": -122.4059143066406\n },\n "分辨率" : 0.5964969992637634\n },\n {\n "海拔": 12.14237785339355,\n "位置": {\n "lat" : 37.78567504882812,\n "lng": -122.4069137573242\n },\n "分辨率": 0.5964969992637634\n },\n {\n "海拔": 12.26948738098145,\n "位置": {\n "lat" : 37.78577423095703,\n "lng" : -122.4069137573242\n },\n "resolution" : 0.5964969992637634\n },\n {\n "elevation" : 13.74397277832031 ,\n "location" : {\n "lat" : 37.78633499145508,\n "lng" : -122.4060211181641\n },\n "resolution" : 0.5964969992637634\n },\n { \n“海拔”:14.1032075881958,\n“位置”:{\n“纬度”:37.78633499145508,\n“经度”:-122.4061889648438\n},\n“分辨率”:0.5964969992637634\n },\n {\n "海拔": 12.31325626373291,\n "位置": {\n "纬度": 37.7858772277832,\n "经度": -122.4069137573242\n },\n "分辨率" : 0.5964969992637634\n },\n {\n "海拔": 12.36202812194824,\n "位置": {\n "lat" : 37.78598022460938,\n "lng" : -122.4069137573242\n },\n "resolution" : 0.5964969992637634\n },\n {\n "elevation" : 14.12999248504639,\n "location" : {\n "lat" : 37.78633499145508,\n "lng" : -122.4063491821289\n },\n "resolution" : 0.5964969992637634\n },\n {\n "高程”:14.4321174621582,\n “位置”:{\n “lat”:37.78633499145508,\n “lng”:-122.4065093994141\n },\n “分辨率”:0.5964969992637634\n } ,\n {\n "海拔": 12.53042221069336,\n "位置": {\n "纬度": 37.78607940673828,\n "经度": -122.4069137573242\n },\n "分辨率": 0 .5964969992637634\n },\n {\n "海拔": 13.14097881317139,\n "位置": {\n "纬度": 37.78618240356445,\n "经度": -122.4069137573242\n }, \n "resolution" : 0.5964969992637634\n },\n {\n "elevation" : 14.39413261413574,\n "location " : {\n "lat" : 37.78633499145508,\n "lng" : -122.4066696166992\n },\n "分辨率" : 0.5964969992637634\n },\n {\n "海拔" : 14.40865802764893,\n "location" : {\n "lat" : 37.78633499145508,\n "lng" : -122.4068374633789\n },\n "resolution" : 0.5964969992637634\n },\ n {\n “海拔”:13.98401737213135,\n “位置”:{\n “纬度”:37.78628158569336,\n “经度”:-122.4069137573242\n },\n “分辨率”:0.5964969992637634\n }\n ],\n "状态" : "OK"\n}\n") Jresponse(分辨率:无,高程:无,位置:无,纬度:无,经度:无) [1]: https://i.stack.imgur.com/uaRZZ.png [2]: https://i.stack.imgur.com/MWJZX.png
根据@Joakim Danielson 的建议,您应该将代码修改为
let jsonResponseProcessed = try! decoder.decode(Jresponse.self, from: data )
根据 google 文档,位于:https://developers.google.com/maps/documentation/elevation/start
你应该使用这个数据结构:EDIT
struct Jresponse: Codable {
let results: [Result]
let status: String?
}
struct Result: Codable {
let elevation: Double
let location: Location
let resolution: Double?
}
struct Location: Codable {
let lat, lng: Double
}
解码失败
您无法获取任何数据的最可能原因是您的数据模型(此处定义为结构 Jresponse
)不符合后端发回的实际 JSON 格式。
我已经快速检查了 google 地图 api 文档的海拔高度,您似乎应该收到一个 json 对象,其中包含顶层的 ElevationResult 数组。然后每个结果将包含分辨率、海拔和位置。
请参考https://developers.google.com/maps/documentation/elevation/overview#ElevationResult
正确的数据模型
struct ElevationResponse: Decodable {
let results: [ElevationResult]
}
struct ElevationResult: Decodable {
let elevation: Double
let location: LatLngLiteral
let resolution: Double?
}
struct LatLngLiteral: Decodable {
let lat: Double
let lng: Double
}
更改解码中的模型
let jsonResponseProcessed = try! decoder.decode(ElevationResponse.self, from: data)
边注
- 解码数据的方式可以更现代化和更清洁
- 注意不要使用force unwrap和force try
- 发布您的问题时,请注意代码缩进,首先 select 您在 Xcode 中的代码,然后执行
ctrl + i
以提高其可读性。然后才将其复制到您的 Whosebug 问题中。