在进一步深入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.
发布于 2014-02-05 11:31:02
JSON响应对象的结构是:
“结果”是JSONArray,包含4 JSONObjects。
这些JSONObjects包含其他JSONObjects,如
"address_components", "formatted_address", "geometry" and "types"
您想要到达JSONObjects "lat“和"lng”,它们位于JSOBObject“位置”,而后者又是JSONObject“几何学”。
整个结构是这样的:
- JSONObject [0]
- JSONObject "address\_components"
- JSONObject "formatted\_address"
- JSONObject "geometry"
- JSOBObject "location"
- JSOBObject "lat" <------- you want this
- JSOBObject "lng" <------- and this
- 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字符串是有效的。
发布于 2014-02-05 10:33:17
答案是错误的:
类型getJsonArray(int)中的方法JSONArray不适用于参数字符串。
它需要一个索引(int),但是您提供了一个键(字符串)。
尝试定位您所追求的参数的索引,并将其传递给它。
或者试试这个:
JSONObject geometry = results.getJSONObject("geometry");
JSONObject location = geometry.getJSONObject("location");
发布于 2014-02-05 10:44:41
键geometry
的值不是JSONArray,而是JSONObject (注意大括号)。因此,getJSONArray()
不能工作。
但是,错误信息有点误导人。
https://stackoverflow.com/questions/21585454
复制相似问题