我在尝试深入挖掘json数据以提取特定键/值对时遇到问题。我只是试图拉动经度/纬度,我认为这是一个位置的子数组,它是几何的子数组。 我的JSON:
results": [
{
"address_components": [
{
"long_name": "56",
"short_name": "56",
"types": [
"street_number"
]
},
{
"long_name": "Rosario Drive",
"short_name": "Rosario Dr",
"types": [
"route"
]
},
{
"long_name": "Waterbury",
"short_name": "Waterbury",
"types": [
"locality",
"political"
]
},
{
"long_name": "New Haven",
"short_name": "New Haven",
"types": [
"administrative_area_level_2",
"political"
]
},
{
"long_name": "Connecticut",
"short_name": "CT",
"types": [
"administrative_area_level_1",
"political"
]
},
{
"long_name": "United States",
"short_name": "US",
"types": [
"country",
"political"
]
},
{
"long_name": "06708",
"short_name": "06708",
"types": [
"postal_code"
]
}
],
"formatted_address": "56 Rosario Drive, Waterbury, CT 06708, USA",
"geometry": {
"bounds": {
"northeast": {
"lat": 41.5291983,
"lng": -73.0523455
},
"southwest": {
"lat": 41.5291845,
"lng": -73.0523456
}
},
"location": {
"lat": 41.5291845,
"lng": -73.0523456
},
"location_type": "RANGE_INTERPOLATED",
"viewport": {
"northeast": {
"lat": 41.5305403802915,
"lng": -73.05099656970849
},
"southwest": {
"lat": 41.5278424197085,
"lng": -73.05369453029151
}
}
},
"types": [
"street_address"
]
},
{
"address_components": [
{
"long_name": "56",
"short_name": "56",
"types": [
"street_number"
]
},
{
"long_name": "Rosario Drive",
"short_name": "Rosario Dr",
"types": [
"route"
]
},
{
"long_name": "Woodbridge",
"short_name": "Woodbridge",
"types": [
"sublocality",
"political"
]
},
{
"long_name": "Vaughan",
"short_name": "Vaughan",
"types": [
"locality",
"political"
]
},
{
"long_name": "York Regional Municipality",
"short_name": "York Regional Municipality",
"types": [
"administrative_area_level_2",
"political"
]
},
{
"long_name": "Ontario",
"short_name": "ON",
"types": [
"administrative_area_level_1",
"political"
]
},
{
"long_name": "Canada",
"short_name": "CA",
"types": [
"country",
"political"
]
},
{
"long_name": "L4H",
"short_name": "L4H",
"types": [
"postal_code_prefix",
"postal_code"
]
}
],
"formatted_address": "56 Rosario Drive, Vaughan, ON L4H, Canada",
"geometry": {
"bounds": {
"northeast": {
"lat": 43.8324404,
"lng": -79.5703888
},
"southwest": {
"lat": 43.83244029999999,
"lng": -79.5704073
}
},
"location": {
"lat": 43.8324404,
"lng": -79.5704073
},
"location_type": "RANGE_INTERPOLATED",
"viewport": {
"northeast": {
"lat": 43.83378933029149,
"lng": -79.5690490697085
},
"southwest": {
"lat": 43.8310913697085,
"lng": -79.57174703029152
}
}
},
"types": [
"street_address"
]
},
{
"address_components": [
{
"long_name": "56",
"short_name": "56",
"types": [
"street_number"
]
},
{
"long_name": "Rosario Drive",
"short_name": "Rosario Dr",
"types": [
"route"
]
},
{
"long_name": "Summerville",
"short_name": "Summerville",
"types": [
"locality",
"political"
]
},
{
"long_name": "Dorchester",
"short_name": "Dorchester",
"types": [
"administrative_area_level_2",
"political"
]
},
{
"long_name": "South Carolina",
"short_name": "SC",
"types": [
"administrative_area_level_1",
"political"
]
},
{
"long_name": "United States",
"short_name": "US",
"types": [
"country",
"political"
]
},
{
"long_name": "29483",
"short_name": "29483",
"types": [
"postal_code"
]
}
],
"formatted_address": "56 Rosario Drive, Summerville, SC 29483, USA",
"geometry": {
"bounds": {
"northeast": {
"lat": 33.0303119,
"lng": -80.2500213
},
"southwest": {
"lat": 33.0302964,
"lng": -80.2500223
}
},
"location": {
"lat": 33.0302964,
"lng": -80.2500213
},
"location_type": "RANGE_INTERPOLATED",
"viewport": {
"northeast": {
"lat": 33.0316531302915,
"lng": -80.24867281970849
},
"southwest": {
"lat": 33.0289551697085,
"lng": -80.25137078029151
}
}
},
"partial_match": true,
"types": [
"street_address"
]
},
{
"address_components": [
{
"long_name": "56",
"short_name": "56",
"types": [
"street_number"
]
},
{
"long_name": "Rosario Drive",
"short_name": "Rosario Dr",
"types": [
"route"
]
},
{
"long_name": "Silver Lake",
"short_name": "Silver Lake",
"types": [
"neighborhood",
"political"
]
},
{
"long_name": "Providence",
"short_name": "Providence",
"types": [
"locality",
"political"
]
},
{
"long_name": "Providence",
"short_name": "Providence",
"types": [
"administrative_area_level_2",
"political"
]
},
{
"long_name": "Rhode Island",
"short_name": "RI",
"types": [
"administrative_area_level_1",
"political"
]
},
{
"long_name": "United States",
"short_name": "US",
"types": [
"country",
"political"
]
},
{
"long_name": "02909",
"short_name": "02909",
"types": [
"postal_code"
]
}
],
"formatted_address": "56 Rosario Drive, Providence, RI 02909, USA",
"geometry": {
"bounds": {
"northeast": {
"lat": 41.8053438,
"lng": -71.4620833
},
"southwest": {
"lat": 41.8053382,
"lng": -71.4621001
}
},
"location": {
"lat": 41.8053382,
"lng": -71.4620833
},
"location_type": "RANGE_INTERPOLATED",
"viewport": {
"northeast": {
"lat": 41.8066899802915,
"lng": -71.46074271970849
},
"southwest": {
"lat": 41.8039920197085,
"lng": -71.46344068029151
}
}
},
"partial_match": true,
"types": [
"street_address"
]
}
],
"status": "OK"
}
我的代码:
if (response != null)
{
JSONObject json;
try {
json = new JSONObject(response);
JSONObject results = json.getJSONObject("results");
JSONArray geometry = results.getJSONArray("geometry");
JSONArray location = geometry.getJSONArray("location");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
错误:
On this line: JSONArray location = geometry.getJSONArray("location");
The method getJsonArray(int) in the type JSONArray is not applicable for the arguments String.
答案 0 :(得分:1)
答案在错误中:
JSONArray类型中的方法getJsonArray(int)不适用 对于参数String。
它需要一个索引(int),但是你提供了一个Key(String)。
尝试找到你所追求的参数的索引,然后传递它。
或试试这个:
JSONObject geometry = results.getJSONObject("geometry");
JSONObject location = geometry.getJSONObject("location");
答案 1 :(得分:1)
键geometry
的值不是JSONArray而是JSONObject(注意花括号)。
因此,getJSONArray()
不起作用。
但错误信息有点误导。
答案 2 :(得分:1)
JSON响应对象的结构是:
“results”是JSONArray,包含4个JSONObjects。
这些JSONObject包含其他JSONObject,例如
"address_components", "formatted_address", "geometry" and "types"
你想要获得JSSOObject“location”中的JSONObjects“lat”和“lng”,它们又是JSONObject“geometry”。
整个结构就是这样的:
JSONArray“结果”
JSONObject [0]
JSONObject [1]
等
要获得lat和lng,你可以这样做:
JSONObject json;
try {
//create main JSONObject
json = new JSONObject(response);
//get results array
JSONArray results = json.getJSONArray("results");
//get first JSONObjects in the array
JSONObject firstObj = results.getJSONObject(0);
//get JSONObject for key "geometry"
JSONObject geometry = firstObj.getJSONObject("geometry");
//get JSONObject for key "location"
JSONObject location = geometry.getJSONObject("location");
//get strings for keys "lat" and "lng"
String lat = location.getString("lat");
String lng = location.getString("lng");
} catch (JSONException e) {
e.printStackTrace();
}
要获取剩余的lat和lng,只需遍历数组中的所有对象,所以:
JSONObject firstObj = results.getJSONObject(1);
JSONObject firstObj = results.getJSONObject(2);
JSONObject firstObj = results.getJSONObject(3);
顺便说一句,json响应开头缺少{
和"
。我想这只是一个遗漏,但您可以检查jsonlint.com上是否有任何JSON字符串有效。