0

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
asked Jan 6, 2020 at 21:04
1
  • What errors are you getting? Commented Jan 6, 2020 at 21:27

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.