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.