Reputation: 4460
I'm trying get a JSONObject from a JSONArray. When I do Log.i display values of JSONArray and has JSONObject that I want. The problem is when I try get this JSONObject, throws an exception show: No value for Local or No value for TipoLocal
. Local and TipoLocal are JSONObject into JSONArray.
How could I do it ?
JSON
{"Retorno":[
{"Local":{"id":"1","nome":"Vovo Landa","telefone":"3333333","celular":"44444","endereco":"Rua 46 a","numero":"025","bairro":"bairro","email":"email"}},
{"TipoLocal":{"id":"1","tipo":"Pizzaria"}}
]}
JSONObject
try{
Gson gson = new Gson();
JSONArray retorno = obj.getJSONArray("Retorno");
if(retorno.length() > 0){
for(int x = 0; x < retorno.length(); x++){
JSONObject jsoObject = retorno.getJSONObject(x);
JSONObject jsoLocal = jsoObject.getJSONObject("Local");
JSONObject jsoTipoLocal = jsoObject.getJSONObject("TipoLocal");
}
}
}catch (JSONException e) {
Log.e("JSONException->:", "getLocais in LocalDAO: " + e.getLocalizedMessage());
}
Upvotes: 0
Views: 454
Reputation: 2670
Change this section :
for(int x = 0; x < retorno.length(); x++){
JSONObject jsoObject = retorno.getJSONObject(x);
JSONObject jsoLocal = jsoObject.getJSONObject("Local");
JSONObject jsoTipoLocal =jsoObject.getJSONObject("TipoLocal");
}
To
for(int x = 0; x < retorno.length(); x++){
JSONObject jsoObject = retorno.getJSONObject(x);
if(jsoObject.has("Local")) {
JSONObject jsoLocal = jsoObject.getJSONObject("Local");
}
if(jsoObject.has("TipoLocal")) {
JSONObject jsoTipoLocal = jsoObject.getJSONObject("TipoLocal");
}
}
Upvotes: 1
Reputation: 338
Their might me following reason when no value found error occur
1)Check your network connection is working properly if you are connecting over Internet.
2)Declare Internet Permission in AndroidManifest.xml file.
3)Check that you are getting proper element like if you are calling result.getJsonObject() then make sure you are trying to get JsonObject not JsonArray.
4)If you are getting No value find in Android then check that you are spelling right word from both android side and server side e.g if you have declare "City" in server then make sure in android or else it should be "City" not "city".
5)If you are adding a JsonObject on JsonArray then check that JsonObject is added on JsonArray on server side.
6)Check the actual exception in LogCat that will tell you actual thing why you are not getting values
Hope this will help you
Upvotes: 0