- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在尝试进一步深入了解 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 [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/
我正在从我的服务器取回以下数据集 {"success":"y","carrier":"Verizon Wireless","isMobile":"true"} 当我尝试深入了解我的 ajax 函数的
我无法引用处于传递给子组件状态的对象和数组的元素。 当我无法在某些子组件中引用它们时,我无法清楚地识别这些情况之间的差异。 我想念什么? 组件的示例属性在组件下给出。发表了评论。例如,我无法引用 cu
我是一名使用 ASP.NET 的网络开发新手。我还没有真正需要 javascript 来做任何事情,因为到目前为止我构建的应用程序都是相当简单的数据挖掘应用程序,我的观众并不在寻找花里胡哨的东西。但是
我在尝试进一步深入了解 json 数据以提取特定的键/值对时遇到问题。我只是想拉出经度/纬度,我认为它是位置的子数组,它是几何的子数组。我的 JSON: results": [ {
Ignoring Block: Either I'm wrong here, or the whiskey is starting to work. (I don't want to rule out
我目前正在使用 C++ 开发一个无线网络应用程序,并且它已经到了我想要在一个进程下多线程处理软件片段的地步,而不是将它们都放在单独的进程中。从理论上讲,我了解多线程,但实际上我还没有深入研究。 每个程
我是一名优秀的程序员,十分优秀!