Hey Guys I have Some problem with Fetch json Array how to get "state"
My 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"
]
}
My 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");
}
BlackHatSamurai
23.6k22 gold badges100 silver badges160 bronze badges
-
What errors are you getting?Christilyn Arjona– Christilyn Arjona2020年01月06日 21:27:07 +00:00Commented Jan 6, 2020 at 21:27
1 Answer 1
Based on your JSON response, your address property is a JSONObject, not a JSONArray:
try{
JSONObject jsonObject = new JSONObject(response);
JSONArray addressObject = jsonObject.getJSONObject("address");
state = addressObject.getString("state");
country= addressObject.getString("country");
}
answered Jan 6, 2020 at 21:33
Christilyn Arjona
2,3033 gold badges16 silver badges21 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
Explore related questions
See similar questions with these tags.
lang-java