【问题标题】:How to parse specific googlemaps API webservices JSON repsonse如何解析特定的谷歌地图 API 网络服务 JSON 响应
【发布时间】:2011-12-26 10:19:18
【问题描述】:

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

{
  "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
        }
      }
    }
  } ]
}

参数latlng在响应中存在多次,例如假设我需要获取location参数的lat/lng

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

我的 JSON 解析代码应该怎么做:

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

这是我写的,解析器如何知道我是明确表示位置参数的纬度/经度还是另一个?

【问题讨论】:

    标签: ios json google-maps


    【解决方案1】:

    为确保获得所需的正确 lat 和 lon 值,您需要获取 results 数组,然后是 results 字典,然后是 geometry 字典,然后是 location字典,像这样:

    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"];
    

    【讨论】:

    • Mutix,谢谢你的回答,你能澄清objectAtIndex:3 的3 指的是什么吗?因为,它与上例中的 Geometry 参数不匹配。
    • 对不起,索引 3 应该是几何字典,但我没有很好地阅读 JSON。我已经相应地更新了我的答案。
    • 哦,是的,非常感谢 Mutix,请原谅我没有将其标记为已接受,我忘记了,您的回答正是我所需要的 :)))))
    【解决方案2】:

    可能没有必要提及,但为了初学者,最后两行应该是:

    double latitude = [[locationDict objectForKey:@"lat"] doubleValue];
    double longitude = [[locationDict objectForKey:@"lng"] doubleValue];
    

    【讨论】:

      【解决方案3】:

      这个函数会给你预期的结果

      - (CLLocationCoordinate2D) geoCodeUsingAddress:(NSString *)address
      {
        double latitude = 0, longitude = 0;
        NSString *straddr =  [address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        NSString *req = [NSString stringWithFormat:@"http://maps.google.com/maps/api/geocode/json?sensor=false&address=%@", straddr];
        NSString *result = [NSString stringWithContentsOfURL:[NSURL URLWithString:req] encoding:NSUTF8StringEncoding error:NULL];
       if (result) 
       {
          NSScanner *scanner = [NSScanner scannerWithString:result];
          if ([scanner scanUpToString:@"\"lat\" :" intoString:nil] && [scanner scanString:@"\"lat\" :" intoString:nil])
          {
              [scanner scanDouble:&latitude];
              if ([scanner scanUpToString:@"\"lng\" :" intoString:nil] && [scanner scanString:@"\"lng\" :" intoString:nil])
              {
                  [scanner scanDouble:&longitude];
              }
          }
      }
      CLLocationCoordinate2D center;
      center.latitude = latitude;
      center.longitude = longitude;
      
      return center;
      
      }
      

      希望这会对某人有所帮助。

      【讨论】:

        猜你喜欢
        • 2011-04-04
        • 2012-10-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-10-10
        • 1970-01-01
        • 2019-06-25
        相关资源
        最近更新 更多