【问题标题】:Parse Google Geocode JSON解析谷歌地理编码 JSON
【发布时间】:2011-09-28 15:32:33
【问题描述】:

我有这个来自 Google GeoCode 的 JSON,我需要对其进行解码,然后从中获取管理区域级别 2 和邮政编码,然后我就完成了。我可以解析结果,但之后我不确定如何重置结果。我试图格式化它,但它不起作用。如果您点击链接,这是格式化版本。

我正在尝试使用 JAVA,我知道我可以在 PHP 中执行此操作,但我更愿意在 JAVA 中执行此操作。提前感谢您的帮助。

http://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&sensor=false

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "279-281",
               "short_name" : "279-281",
               "types" : [ "street_number" ]
            },
            {
               "long_name" : "Bedford Ave",
               "short_name" : "Bedford Ave",
               "types" : [ "route" ]
            },
            {
               "long_name" : "Williamsburg",
               "short_name" : "Williamsburg",
               "types" : [ "neighborhood", "political" ]
            },
            {
               "long_name" : "Brooklyn",
               "short_name" : "Brooklyn",
               "types" : [ "sublocality", "political" ]
            },
            {
               "long_name" : "Kings",
               "short_name" : "Kings",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "New York",
               "short_name" : "NY",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "United States",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "11211",
               "short_name" : "11211",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "279-281 Bedford Ave, Brooklyn, NY 11211, USA",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 40.71450470,
                  "lng" : -73.96128740
               },
               "southwest" : {
                  "lat" : 40.7139010,
                  "lng" : -73.961680
               }
            },
            "location" : {
               "lat" : 40.71422150,
               "lng" : -73.96144540
            },
            "location_type" : "RANGE_INTERPOLATED",
            "viewport" : {
               "northeast" : {
                  "lat" : 40.71735047068017,
                  "lng" : -73.95833607931984
               },
               "southwest" : {
                  "lat" : 40.71105522931983,
                  "lng" : -73.96463132068017
               }
            }
         },
         "types" : [ "street_address" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "Williamsburg",
               "short_name" : "Williamsburg",
               "types" : [ "neighborhood", "political" ]
            },
            {
               "long_name" : "New York",
               "short_name" : "New York",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Kings",
               "short_name" : "Kings",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "New York",
               "short_name" : "NY",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "United States",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "Williamsburg, NY, USA",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 40.7182050,
                  "lng" : -73.9202810
               },
               "southwest" : {
                  "lat" : 40.6979330,
                  "lng" : -73.96984510
               }
            },
            "location" : {
               "lat" : 40.70644610,
               "lng" : -73.95361629999999
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 40.7182050,
                  "lng" : -73.9202810
               },
               "southwest" : {
                  "lat" : 40.6979330,
                  "lng" : -73.96984510
               }
            }
         },
         "types" : [ "neighborhood", "political" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "11211",
               "short_name" : "11211",
               "types" : [ "postal_code" ]
            },
            {
               "long_name" : "Brooklyn",
               "short_name" : "Brooklyn",
               "types" : [ "sublocality", "political" ]
            },
            {
               "long_name" : "New York",
               "short_name" : "New York",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "New York",
               "short_name" : "NY",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "United States",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "Brooklyn, NY 11211, USA",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 40.7261370,
                  "lng" : -73.92304290
               },
               "southwest" : {
                  "lat" : 40.69763590,
                  "lng" : -73.97616690
               }
            },
            "location" : {
               "lat" : 40.71800360,
               "lng" : -73.96537150000002
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 40.7261370,
                  "lng" : -73.92304290
               },
               "southwest" : {
                  "lat" : 40.69763590,
                  "lng" : -73.97616690
               }
            }
         },
         "types" : [ "postal_code" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "Brooklyn",
               "short_name" : "Brooklyn",
               "types" : [ "sublocality", "political" ]
            },
            {
               "long_name" : "New York",
               "short_name" : "New York",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Kings",
               "short_name" : "Kings",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "New York",
               "short_name" : "NY",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "United States",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "Brooklyn, NY, USA",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 40.7394460,
                  "lng" : -73.8333650
               },
               "southwest" : {
                  "lat" : 40.55104190,
                  "lng" : -74.056630
               }
            },
            "location" : {
               "lat" : 40.650,
               "lng" : -73.950
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 40.7394460,
                  "lng" : -73.8333650
               },
               "southwest" : {
                  "lat" : 40.55104190,
                  "lng" : -74.056630
               }
            }
         },
         "types" : [ "sublocality", "political" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "Kings",
               "short_name" : "Kings",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "New York",
               "short_name" : "NY",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "United States",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "Kings, New York, USA",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 40.7394460,
                  "lng" : -73.8333650
               },
               "southwest" : {
                  "lat" : 40.55104190,
                  "lng" : -74.056630
               }
            },
            "location" : {
               "lat" : 40.65287620,
               "lng" : -73.95949399999999
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 40.7394460,
                  "lng" : -73.8333650
               },
               "southwest" : {
                  "lat" : 40.55104190,
                  "lng" : -74.056630
               }
            }
         },
         "types" : [ "administrative_area_level_2", "political" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "New York",
               "short_name" : "New York",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "New York",
               "short_name" : "New York",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "New York",
               "short_name" : "NY",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "United States",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "New York, NY, USA",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 40.9175770,
                  "lng" : -73.7002720
               },
               "southwest" : {
                  "lat" : 40.4773990,
                  "lng" : -74.259090
               }
            },
            "location" : {
               "lat" : 40.71435280,
               "lng" : -74.00597309999999
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 40.9175770,
                  "lng" : -73.7002720
               },
               "southwest" : {
                  "lat" : 40.4773990,
                  "lng" : -74.259090
               }
            }
         },
         "types" : [ "locality", "political" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "New York",
               "short_name" : "NY",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "United States",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "New York, USA",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 45.0158650,
                  "lng" : -71.7774910
               },
               "southwest" : {
                  "lat" : 40.4773990,
                  "lng" : -79.762590
               }
            },
            "location" : {
               "lat" : 43.29942850,
               "lng" : -74.21793260000001
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 45.0158650,
                  "lng" : -71.7774910
               },
               "southwest" : {
                  "lat" : 40.4773990,
                  "lng" : -79.762590
               }
            }
         },
         "types" : [ "administrative_area_level_1", "political" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "United States",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "United States",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 71.53879999999999,
                  "lng" : -66.88507489999999
               },
               "southwest" : {
                  "lat" : 18.77630,
                  "lng" : 170.59570
               }
            },
            "location" : {
               "lat" : 37.090240,
               "lng" : -95.7128910
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 71.53879999999999,
                  "lng" : -66.88507489999999
               },
               "southwest" : {
                  "lat" : 18.77630,
                  "lng" : 170.59570
               }
            }
         },
         "types" : [ "country", "political" ]
      }
   ],
   "status" : "OK"
}

【问题讨论】:

  • 尝试过使用 JSON 解析库吗?
  • 请参阅stackoverflow.com/questions/1688099/converting-json-to-java/… 以获取有关在 java 中解析 json 的提示。
  • 我可以毫无错误地解析它,并以字符串的形式获得上述结果。我在提取邮政编码和管理员级别 2 时遇到问题。这是我没有得到的部分。
  • @Brandon Wilson,将代码发布在您实际解析输入的位置可能是个好主意。
  • “获得管理区域级别_2”是什么意思? “administrative_area_level_2”的值是多少?是“国王”、“国王”吗?

标签: java json google-maps-api-3


【解决方案1】:

我设法弄明白了。我写了一些代码来遍历每个对象,直到我从字符串中得到我需要的东西。我将在今天晚些时候发布代码。谢谢您的帮助。它只是很多数据,我想也许有人会有更简单的方法。

【讨论】:

  • 没有代码需要的家伙。他解释了他采取的方法和方式。这是一个可以帮助人们解决这个问题的提示,所以不要投反对票!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-06
  • 2012-05-30
  • 1970-01-01
  • 2010-10-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多