1

I am trying to get data from this JSON file like this: http://api.worldbank.org/countries/ir?format=json

If you have a look at the file by a JSON editor(like this), you will see that there are arrays and objects inside arrays. I can Only get access to the first level. But how to access to deepr levels. Here what I have done so far:

            JSONArray array = json.getJSONArray("data");
            JSONObject c = array.getJSONObject(0);

            JSONArray array2 = array.getJSONArray(1);
            JSONObject c2 = array2.getJSONObject(0);



            //Storing JSON item in a Variable
            String name = c2.getString(NAME);
            String capitalCity = c2.getString(CAPITALCITY);

So by this lines of codes I can get the country name and the capital city.

But in the same array there is another object called region which it has two other strings, id and value . How can I access inside region and get the value?

2 Answers 2

1

Field region is a JSONObject again:

JSONObject region = c2.getJSONObject("region");
String regionId = region.getString("id");
String regionValue = region.getString("value");
Sign up to request clarification or add additional context in comments.

Comments

1

You can try this:

JSONObject regionJsObj = c2.getJSONObject("region");
String regionId = regionJsObj.getString("id");
String regionValue = regionJsObj.getString("value");

you can do the same for all the jsonObjects that inside jsonObject as "adminregion" or "incomeLevel" or "lendingType" or so...

Comments

Your Answer

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.