gpt4 book ai didi

java - 深入研究 Android JSON 数据数组

转载 作者:行者123 更新时间:2023-12-01 23:12:59 26 4
gpt4 key购买 nike

我在尝试进一步深入了解 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.

最佳答案

JSON 响应对象的结构是:

“结果”是包含 4 个 JSONObject 的 JSONArray。

这些 JSONObject 包含其他 JSONObject,例如

"address_components", "formatted_address", "geometry" and "types"

您想要获取位于 JSOBObject“location”中的 JSONObjects“lat”和“lng”,而后者又位于 JSONObject“geometry”中。

整个结构是这样的:

  • JSONArray“结果”

    • JSONObject [0]

      • JSONObject“地址组件”
      • JSONObject“formatted_address”
      • JSONObject“几何”
        • JSOBObject“位置”
          • JSOBObject“lat”<--------你想要这个
          • JSOBObject "lng"<-------- 和这个
    • JSONObject [1]

    等等

要获取纬度和经度,您可以这样做:

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();
}

要获取剩余的纬度和经度,只需遍历数组中的所有对象即可,因此:

JSONObject firstObj = results.getJSONObject(1);
JSONObject firstObj = results.getJSONObject(2);
JSONObject firstObj = results.getJSONObject(3);

顺便说一句,您的 json 响应的开头缺少 {"。我想这只是一个遗漏,但您可以检查是否有任何 JSON 字符串在 jsonlint.com 有效。

关于java - 深入研究 Android JSON 数据数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21585454/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com