如何解析特定的Googlemaps API Web服务JSON repsonse [英] How to parse specific googlemaps API webservices JSON repsonse

查看:86
本文介绍了如何解析特定的Googlemaps API Web服务JSON repsonse的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Google Maps API JSON响应返回具有相同名词的不同参数:

The google Maps API JSON response return different parameters with the same noun:

{
  "status": "OK",
  "results": [ {
    "types": [ "street_address" ],
    "formatted_address": "1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA",
    "address_components": [ {
      "long_name": "1600",
      "short_name": "1600",
      "types": [ "street_number" ]
    }, {
      "long_name": "Amphitheatre Pkwy",
      "short_name": "Amphitheatre Pkwy",
      "types": [ "route" ]
    }, {
      "long_name": "Mountain View",
      "short_name": "Mountain View",
      "types": [ "locality", "political" ]
    }, {
      "long_name": "California",
      "short_name": "CA",
      "types": [ "administrative_area_level_1", "political" ]
    }, {
      "long_name": "United States",
      "short_name": "US",
      "types": [ "country", "political" ]
    }, {
      "long_name": "94043",
      "short_name": "94043",
      "types": [ "postal_code" ]
    } ],
    "geometry": {
      "location": {
        "lat": 37.4219720,
        "lng": -122.0841430
      },
      "location_type": "ROOFTOP",
      "viewport": {
        "southwest": {
          "lat": 37.4188244,
          "lng": -122.0872906
        },
        "northeast": {
          "lat": 37.4251196,
          "lng": -122.0809954
        }
      }
    }
  } ]
}

参数 lat 和<$ c例如,假设我需要获取位置参数的 lat / lng / p>

The parameter lat and lng exists multiple times in the response, for example, assuming that i need to get the lat/lng of the location parameter:

"location": {
            "lat": 37.4219720,
            "lng": -122.0841430
          },

我应该为我的JSON解析器做些什么g代码:

What should i do for my JSON parsing code:

NSDictionary *responseDict = [responseString JSONValue];
    double latitude = [responseDict objectForKey:@"lat"];
    double longitude = [responseDict objectForKey:@"lng"];

这就是我写的,解析器如何知道我的意思是明确的lat / lng位置参数还是另一个?

This is what i write, how does the parser know if i mean explicitly the lat/lng of the location parameter or another one?

推荐答案

为了确保获得正确的lat和lon值,您需要得到结果数组,然后结果字典,然后几何 dictionary,然后是 location 字典,如下所示:

To make sure you get the right lat and lon values that you are after, you would need to get the results array, then the results dictionary, then the geometry dictionary, followed by the location dictionary, like so:

NSDictionary *responseDict = [responseString JSONValue];

// The "results" object is an array that contains a single dictionary: 
// "results": [{...}]
// So first lets get the results array
NSArray *resultsArray = [responseDict objectForKey:@"results"];

// Then get the results dictionary
NSDictionary *resultsDict = [resultsArray objectAtIndex:0];

// Once we have the results dictionary, we can get the geometry
NSDictionary *geometryDict = [resultsDict objectForKey:@"geometry"];

// Then we get the location
NSDictionary *locationDict = [geometryDict objectForKey:@"location"];

// Now we can get the latitude and longitude
double latitude = [locationDict objectForKey:@"lat"];
double longitude = [locationDict objectForKey:@"lng"];

这篇关于如何解析特定的Googlemaps API Web服务JSON repsonse的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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