How I can get "name" under the "language" attribute of this JSONArray?

Entomic :

I use a Web-API for a Android App. But I have a problem to get "name" from the attribute "languages".

For the "name" of country I used:

protected String parseJSON(String jsonString) throws Exception {

    if (jsonString == null || jsonString.trim().length() == 0) {

        return "empty Object";
    }
    JSONArray jsonarray = new JSONArray(jsonString);
    JSONObject jsonObject = jsonarray.getJSONObject(0);

    String name = jsonObject.getString("name");

    return name;
}

How I can get "name" under the "language" attribute of this JSONArray? https://restcountries.eu/rest/v2/name/spain

jeprubio :

It should be something like this:

JSONArray languages = jsonObject.getJSONArray("languages");
String firstLanguageName = languages.getJSONObject(0).getString("name");

Even though I strongly encourage you using gson instead of parsing this way, gson makes life easier when parsing json.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=169510&siteId=1