我有以下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)
}
}
}
}
}
我的问题是,我不确定如何提取我想要的部分,这是' lat'和' lng'在第一场比赛的位置部分下。我正在使用Alamofire和SwiftyJSON
编辑:JSON正确回归,因此没有问题。
答案 0 :(得分:2)
根对象是包含键status
和results
的字典。您必须检查status
是否“正常”并获取密钥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,关于结构的列表非常明确。
[]
代表一个数组。 {}
代表字典。String
。Int
,Double
(包括点)或Bool
(true
或false
)。null
被桥接到NSNull
。