Reputation: 357
I'm trying to retrieve the "Designacao" text to a listview, the problem is I have an array (paises) inside other array (no name BTW), how can I do it?
JSON:
[
{
paises: [
{
K_PAIS: "mz",
K_CONTINENTE: 2,
K_LINGUAGEM: "pt",
Designacao: "Moçambique",
URL_IMAGE_LARGE: "/Images/large-flags/mz-large.jpg",
URL_IMAGE_SMALL: "/Images/flags/mz.png",
Coord_LAT: -18.4335022,
Coord_LONG: 30.9779447,
Coord_Zoom: 6,
Status: "OK"
},
{
K_PAIS: "ma",
K_CONTINENTE: 2,
K_LINGUAGEM: "pt",
Designacao: "Marrocos",
URL_IMAGE_LARGE: "/Images/large-flags/ma-large.jpg",
URL_IMAGE_SMALL: "/Images/flags/ma.png",
Coord_LAT: 31.7252847,
Coord_LONG: -11.5777195,
Coord_Zoom: 6,
Status: "OK"
},
{
K_PAIS: "dz",
K_CONTINENTE: 2,
K_LINGUAGEM: "pt",
Designacao: "Argélia",
URL_IMAGE_LARGE: "/Images/large-flags/dz-large.jpg",
URL_IMAGE_SMALL: "/Images/flags/dz.png",
Coord_LAT: 27.7538925,
Coord_LONG: -7.3521369,
Coord_Zoom: 5,
Status: "OK"
}
],
K_CONTINENTE: 2,
K_LINGUAGEM: "pt",
Designacao: "África",
URL_IMAGE_LARGE: "/Images/large-flags/world-large.png",
URL_IMAGE_SMALL: "/Images/flags/world.png",
Coord_LAT: 3.6906886,
Coord_LONG: 12.63657,
Coord_Zoom: 3,
Data_Criacao: "2017-03-01T00:00:00",
Data_Alteracao: "2017-03-01T00:00:00",
Status: "OK"
},
{
paises: [
{
K_PAIS: "us",
K_CONTINENTE: 3,
K_LINGUAGEM: "pt",
Designacao: "Estados Unidos da América",
URL_IMAGE_LARGE: "/Images/large-flags/us-large.jpg",
URL_IMAGE_SMALL: "/Images/flags/us.png",
Coord_LAT: 37.09024,
Coord_LONG: -95.712891,
Coord_Zoom: 4,
Status: "OK"
},
{
K_PAIS: "mx",
K_CONTINENTE: 3,
K_LINGUAGEM: "pt",
Designacao: "México",
URL_IMAGE_LARGE: "/Images/large-flags/mx-large.jpg",
URL_IMAGE_SMALL: "/Images/flags/mx.png",
Coord_LAT: 23.3853163,
Coord_LONG: -111.5655016,
Coord_Zoom: 5,
Status: "OK"
}
],
K_CONTINENTE: 3,
K_LINGUAGEM: "pt",
Designacao: "América do Norte",
URL_IMAGE_LARGE: "/Images/large-flags/world-large.png",
URL_IMAGE_SMALL: "/Images/flags/world.png",
Coord_LAT: 54.5259614,
Coord_LONG: -105.2551187,
Coord_Zoom: 4,
Data_Criacao: "2017-03-01T00:00:00",
Data_Alteracao: "2017-03-01T00:00:00",
Status: "OK"
},
{},
{},
{},
{}
]
This is the important part of the java code on the mainactivity:
JSONArray jsonArray = new JSONArray();
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObj = new JSONObject(jsonStr);
JSONArray paises = jsonObj.getJSONArray("paises");
//loop
for (int h = 0; h < paises.length(); h++) {
JSONObject c = paises.getJSONObject(h);
String K_PAIS = c.getString("K_PAIS");
String Designacao = c.getString("Designcao");
HashMap<String, String> pais = new HashMap<>();
pais.put("K_PAIS", K_PAIS);
pais.put("Designacao", Designacao);
listaPaises.add(pais);
}
}
My code is largely based in: http://www.androidhive.info/2012/01/android-json-parsing-tutorial/
Edited content:
@Override
protected Void doInBackground(Void... params) {
HttpHandler sh = new HttpHandler();
String jsonStr = sh.makeServiceCall(url);
Log.e(TAG, "Response from URL: " + jsonStr);
if (jsonStr != null) {
try {
JSONArray array = new JSONArray(jsonStr);
for (int i = 0; i < array.length(); i++) {
JSONObject jsonObject = array.getJSONObject(i);
JSONArray paises = jsonObject.optJSONArray("paises");
if (paises != null) {
for (int j = 0; j < paises.length(); j++) {
JSONObject jsonObject1 = paises.getJSONObject(j);
System.out.println(jsonObject1.optString("Designacao"));
String K_PAIS = jsonObject1.getString("K_PAIS");
String Designacao = jsonObject1.getString("Designcao");
HashMap<String, String> pais = new HashMap<>();
pais.put("K_PAIS", K_PAIS);
pais.put("Designacao", Designacao);
listaPaises.add(pais);
}
}
System.out.println(jsonObject.optString("Designacao"));
}
} catch (final JSONException e) {
Log.e(TAG, "Json parsing error: " + e.getMessage());
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), "Json parsin error: " + e.getMessage(), Toast.LENGTH_LONG).show();
}
});
}
} else {
Log.e(TAG, "Couldn't get json from server.");
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), "Couldn't get json from server. Check LogCat for possible errpr!", Toast.LENGTH_LONG).show();
}
});
}
return null;
}
Upvotes: 1
Views: 87
Reputation: 37404
Issues
1.) your response is a JSONArray
not JSONObject
new JSONObject(jsonStr); // error
2.) Some of your JSONObjects
are empty so you need to check if your JSONObject
contain any paises
array.
3.) Nested arrays mean nested loops
// JSONArray response
JSONArray array = new JSONArray(jsonStr);
// traverse your array objects
for (int i = 0; i < array.length(); i++) {
// fetch object one by one using index i
JSONObject jsonObject =array.getJSONObject(i);
// optJSONArray will give you null if there is no paises jsonArray
JSONArray paises =jsonObject.optJSONArray("paises");
if (paises!=null) { // nullity check
for (int j = 0; j < paises.length(); j++) { // nested loop for paises array
// fetch paises array objects
JSONObject jsonObject1 = paises.getJSONObject(j);
// ahh ! this is what you were looking for
// optString give you "" empty string if there is no value
System.out.println(jsonObject1.optString("Designacao"));
}
}
// fetch value which is outside paises array
System.out.println(jsonObject.optString("Designacao"));
}
Output :
Moçambique
Marrocos
Argélia
Ã?frica
Estados Unidos da América
México
América do Norte
Note : avoid some weird ?
etc system font issue
Upvotes: 2