Swift 3-提取JSON数据 [英] Swift 3 - Extract JSON data

查看:116
本文介绍了Swift 3-提取JSON数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下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"
}

以及以下要调用的代码,并尝试从中提取所需的数据:

And the following code to call and try to extract the data I need from it:

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

My problem is I'm not sure how to extract the part I want, which is 'lat' and 'lng' under the location section of the first match. I am using Alamofire and SwiftyJSON

JSON正确返回,因此没有问题.

The JSON is coming back correctly so there is no issue there.

推荐答案

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

The root object is a dictionary containing the keys status and results. You have to check if the status is "OK" and get the array for key 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,有关结构的清单非常清楚.

Read the JSON carefully, the listing is very clear regarding the structure.

  • []表示一个数组.
  • {}代表字典.
  • 双引号中的值为String.
  • 不带双引号的值是IntDouble(包括点)或Bool(truefalse).
  • null被桥接到NSNull.
  • [] represents an array.
  • {} represents a dictionary.
  • values in double quotes are String.
  • values without double quotes are Int, Double (including dot) or Bool (true or false).
  • null is bridged to NSNull.

这篇关于Swift 3-提取JSON数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆