Volley jsonArray 传递
Volley jsonArray Pass
嘿伙计们,我有一些关于 Fetch json 数组如何获取的问题 "state"
我的JSON:
{
"place_id": 285711754,
"licence": "Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright",
"osm_type": "way",
"osm_id": 338974979,
"lat": "22.3853080529788",
"lon": "33.515458351351",
"place_rank": 26,
"category": "highway",
"type": "unclassified",
"importance": 0.1,
"addresstype": "road",
"name": null,
"display_name": "Red Sea Governorate, Egypt",
"address": {
"state": "Red Sea Governorate",
"country": "Egypt",
"country_code": "eg"
},
"boundingbox": [
"22.298938",
"22.4798642",
"33.4408912",
"33.5530448"
]
}
我的Java:
try{
JSONObject jsonObject = new JSONObject(response);
JSONArray postsArray = jsonObject.getJSONArray("address");
for (int i = 0 ; i<postsArray.length() ;i++) {
JSONObject postObject = postsArray.getJSONObject(i);
state= postObject.getString("state");
country= postObject.getString("country");
}
try{
JSONObject jsonObject = new JSONObject(response);//my problem
JSONArray postsArray = jsonObject.getJSONArray("address");
JSONObject postObject = postsArray.getJSONObject(0);
state= postObject.getString("state");
country= postObject.getString("country");
}
根据您的 JSON 回复,您的 address
属性 是 JSONObject
,而不是 JSONArray
:
try{
JSONObject jsonObject = new JSONObject(response);
JSONArray addressObject = jsonObject.getJSONObject("address");
state = addressObject.getString("state");
country= addressObject.getString("country");
}
嘿伙计们,我有一些关于 Fetch json 数组如何获取的问题 "state"
我的JSON:
{
"place_id": 285711754,
"licence": "Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright",
"osm_type": "way",
"osm_id": 338974979,
"lat": "22.3853080529788",
"lon": "33.515458351351",
"place_rank": 26,
"category": "highway",
"type": "unclassified",
"importance": 0.1,
"addresstype": "road",
"name": null,
"display_name": "Red Sea Governorate, Egypt",
"address": {
"state": "Red Sea Governorate",
"country": "Egypt",
"country_code": "eg"
},
"boundingbox": [
"22.298938",
"22.4798642",
"33.4408912",
"33.5530448"
]
}
我的Java:
try{
JSONObject jsonObject = new JSONObject(response);
JSONArray postsArray = jsonObject.getJSONArray("address");
for (int i = 0 ; i<postsArray.length() ;i++) {
JSONObject postObject = postsArray.getJSONObject(i);
state= postObject.getString("state");
country= postObject.getString("country");
}
try{
JSONObject jsonObject = new JSONObject(response);//my problem
JSONArray postsArray = jsonObject.getJSONArray("address");
JSONObject postObject = postsArray.getJSONObject(0);
state= postObject.getString("state");
country= postObject.getString("country");
}
根据您的 JSON 回复,您的 address
属性 是 JSONObject
,而不是 JSONArray
:
try{
JSONObject jsonObject = new JSONObject(response);
JSONArray addressObject = jsonObject.getJSONObject("address");
state = addressObject.getString("state");
country= addressObject.getString("country");
}