gpt4 book ai didi

json - Swift 3 - 提取 JSON 数据

转载 作者:行者123 更新时间:2023-11-28 12:34:20 25 4
gpt4 key购买 nike

我有以下 JSON

{
"results" : [
{
"address_components" : [
{
"long_name" : "OL12 7LD",
"short_name" : "OL12 7LD",
"types" : [ "postal_code" ]
},
{
"long_name" : "Ings Lane",
"short_name" : "Ings Ln",
"types" : [ "route" ]
},
{
"long_name" : "Rochdale",
"short_name" : "Rochdale",
"types" : [ "postal_town" ]
},
{
"long_name" : "Greater Manchester",
"short_name" : "Greater Manchester",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "England",
"short_name" : "England",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United Kingdom",
"short_name" : "GB",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Ings Ln, Rochdale OL12 7LD, UK",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 53.6307898,
"lng" : -2.1838913
},
"southwest" : {
"lat" : 53.62996279999999,
"lng" : -2.1857861
}
},
"location" : {
"lat" : 53.63032279999999,
"lng" : -2.1847775
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 53.6317252802915,
"lng" : -2.183489719708498
},
"southwest" : {
"lat" : 53.6290273197085,
"lng" : -2.186187680291502
}
}
},
"place_id" : "ChIJSzEfAAi8e0gREDjvGtRJwLM",
"types" : [ "postal_code" ]
},
{
"address_components" : [
{
"long_name" : "OL12",
"short_name" : "OL12",
"types" : [ "postal_code", "postal_code_prefix" ]
},
{
"long_name" : "Rochdale",
"short_name" : "Rochdale",
"types" : [ "postal_town" ]
},
{
"long_name" : "England",
"short_name" : "England",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United Kingdom",
"short_name" : "GB",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Rochdale OL12, UK",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 53.6895598,
"lng" : -2.1106483
},
"southwest" : {
"lat" : 53.6127321,
"lng" : -2.2874164
}
},
"location" : {
"lat" : 53.66276999999999,
"lng" : -2.187286
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 53.6895598,
"lng" : -2.1106483
},
"southwest" : {
"lat" : 53.6127321,
"lng" : -2.2874164
}
}
},
"place_id" : "ChIJt3p6tTS5e0gRCt658WmnJVM",
"types" : [ "postal_code", "postal_code_prefix" ]
},
{
"address_components" : [
{
"long_name" : "Northwest Edmond Airport",
"short_name" : "Northwest Edmond Airport",
"types" : [ "establishment", "point_of_interest" ]
},
{
"long_name" : "Edmond",
"short_name" : "Edmond",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Oklahoma County",
"short_name" : "Oklahoma County",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Oklahoma",
"short_name" : "OK",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United States",
"short_name" : "US",
"types" : [ "country", "political" ]
},
{
"long_name" : "73025",
"short_name" : "73025",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "Northwest Edmond Airport, Edmond, OK 73025, USA",
"geometry" : {
"location" : {
"lat" : 35.7075513,
"lng" : -97.54059719999999
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 35.70890028029149,
"lng" : -97.53924821970848
},
"southwest" : {
"lat" : 35.7062023197085,
"lng" : -97.5419461802915
}
}
},
"place_id" : "ChIJvTkNtm72sYcRZRzH8Qx2q_o",
"types" : [ "airport", "establishment", "point_of_interest" ]
}
],
"status" : "OK"
}

下面的代码调用并尝试从中提取我需要的数据:

Alamofire.request(requestURL).responseString { response in
debugPrint(response)

if let json = response.result.value {
if let data = json.data(using: String.Encoding.utf8) {
let json = JSON(data: data)
for result in json["location"].arrayValue {
print(result["lat"].stringValue)
print(result["lng"].stringValue)
}
}


}
}
}

我的问题是我不确定如何提取我想要的部分,即第一个匹配项的位置部分下的“纬度”和“经度”。我正在使用 Alamofire 和 SwiftyJSON

编辑:JSON 正确返回,因此没有问题。

最佳答案

根对象是一个包含关键字statusresults 的字典。您必须检查 status 是否为“OK”并获取关键 results 的数组:

if let status = json["status"].string, status == "OK", let results = json["results"].array {
for result in results {
if let location = result["geometry"]["location"].dictionary {
let longitude = location["lng"]!.doubleValue
let latitude = location["lat"]!.doubleValue
print(longitude, latitude)
}

}
}

仔细阅读JSON, list 结构非常清晰。

  • [] 代表一个数组。
  • {} 代表字典。
  • 双引号中的值为 String
  • 不带双引号的值为 IntDouble(包括点)或 Bool(true).
  • null 桥接到 NSNull

关于json - Swift 3 - 提取 JSON 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41302397/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com