Swift 3 - Extract JSON data

徘徊边缘 提交于 2019-12-12 04:23:35

问题


I have the following 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)
                    }
            }


    }
}
}

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

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


回答1:


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)
        }

    }
}

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

  • [] 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.


来源:https://stackoverflow.com/questions/41302397/swift-3-extract-json-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!