Cant get data from JSON Object (Android)

I cant parse a JSON response from an url ("http://www.telekino.com.ar/cupones/control/1267/1027027/84.json")

{"kino":{"ganador":false,"diferencia":""},"rekino":{"ganador":false,"diferencia":""},"carton_formateado":"1.027.027","cupon_numeros":"","premio_extra":false,"cupon_caducidad":"22\/08\/2016","cupon_erroneo":true}

I used a JSONViewr to understand the tree, and is very simple, but I cant get the booleans, strings, etc. I allways recieve null results

JsonURL= "http://www.telekino.com.ar/cupones/control/1267/1027027/84.json"



            JsonObjectRequest jsObjRequest = new JsonObjectRequest
                    (Request.Method.GET, JsonURL, null, new Response.Listener<JSONObject>() {

                        @Override
                        public void onResponse(JSONObject response) {





                                try {
                                    JSONObject object= response.getJSONObject("object");

                                    algoerroneo = object.getBoolean("algo_erroneo");
                                    JSONObject kino = object.getJSONObject("kino");
                                    kinoganador = kino.getBoolean("ganador");
                                    JSONObject data = kino.getJSONObject("data");
                                    JSONObject Categoria = data.getJSONObject("Categoria");
                                    Smonto = Categoria.getString("monto");
                                    JSONObject Ganador = data.getJSONObject("Ganador");
                                    Saciertos = Ganador.getString("aciertos");
                                    JSONObject rekino = object.getJSONObject("rekino");
                                    rekinoganador = rekino.getBoolean("ganador");
                                    objetoslist = object.toString();





                                } catch (JSONException e1) {
                                    e1.printStackTrace();

                                }








                            data=String.valueOf(algoerroneo)+" "+String.valueOf(kinoganador)+" "+String.valueOf(rekinoganador)+ "\n"+objetoslist;
                            results.setText(data);

                        }
                    }, new Response.ErrorListener() {

                        @Override
                        public void onErrorResponse(VolleyError error) {
                            // TODO Auto-generated method stub
                            text8.setText("shit2");
                        }
                    });

                    // Access the RequestQueue through your singleton class.
                    MySingleton.getInstance(MainActivity.this).addToRequestQueue(jsObjRequest);

You can create class from JSON (using http://www.jsonschema2pojo.org/):

Result:

    -----------------------------------com.example.Example.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Example {

@SerializedName("kino")
@Expose
private Kino kino;
@SerializedName("rekino")
@Expose
private Rekino rekino;
@SerializedName("carton_formateado")
@Expose
private String cartonFormateado;
@SerializedName("cupon_numeros")
@Expose
private String cuponNumeros;
@SerializedName("premio_extra")
@Expose
private Boolean premioExtra;
@SerializedName("cupon_caducidad")
@Expose
private String cuponCaducidad;
@SerializedName("cupon_erroneo")
@Expose
private Boolean cuponErroneo;

public Kino getKino() {
return kino;
}

public void setKino(Kino kino) {
this.kino = kino;
}

public Rekino getRekino() {
return rekino;
}

public void setRekino(Rekino rekino) {
this.rekino = rekino;
}

public String getCartonFormateado() {
return cartonFormateado;
}

public void setCartonFormateado(String cartonFormateado) {
this.cartonFormateado = cartonFormateado;
}

public String getCuponNumeros() {
return cuponNumeros;
}

public void setCuponNumeros(String cuponNumeros) {
this.cuponNumeros = cuponNumeros;
}

public Boolean getPremioExtra() {
return premioExtra;
}

public void setPremioExtra(Boolean premioExtra) {
this.premioExtra = premioExtra;
}

public String getCuponCaducidad() {
return cuponCaducidad;
}

public void setCuponCaducidad(String cuponCaducidad) {
this.cuponCaducidad = cuponCaducidad;
}

public Boolean getCuponErroneo() {
return cuponErroneo;
}

public void setCuponErroneo(Boolean cuponErroneo) {
this.cuponErroneo = cuponErroneo;
}

}
-----------------------------------com.example.Kino.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Kino {

@SerializedName("ganador")
@Expose
private Boolean ganador;
@SerializedName("diferencia")
@Expose
private String diferencia;

public Boolean getGanador() {
return ganador;
}

public void setGanador(Boolean ganador) {
this.ganador = ganador;
}

public String getDiferencia() {
return diferencia;
}

public void setDiferencia(String diferencia) {
this.diferencia = diferencia;
}

}
-----------------------------------com.example.Rekino.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Rekino {

@SerializedName("ganador")
@Expose
private Boolean ganador;
@SerializedName("diferencia")
@Expose
private String diferencia;

public Boolean getGanador() {
return ganador;
}

public void setGanador(Boolean ganador) {
this.ganador = ganador;
}

public String getDiferencia() {
return diferencia;
}

public void setDiferencia(String diferencia) {
this.diferencia = diferencia;
}

}

Then

JsonURL= "http://www.telekino.com.ar/cupones/control/1267/1027027/84.json"

    JsonObjectRequest jsObjRequest = new JsonObjectRequest
            (Request.Method.GET, JsonURL, null, new Response.Listener<JSONObject>() {

                @Override
                public void onResponse(JSONObject response) {





                    try {
                       // JSONObject object= response.getJSONObject("object");


                        JSONObject kinoObject = object.getJSONObject("kino");
                        JSONObject rekinoObject = object.getJSONObject("rekino");


                        Gson gson = new Gson();

                        Kino  kino = gson.fromJson(kinoObject.toString(), Kino.class);
                        Rekino  rekino = gson.fromJson(rekinoObject.toString(), Rekino.class);
                        Example example = gson.fromJson(response.toString(), Example.class);



                    } catch (JSONException e1) {
                        e1.printStackTrace();

                    }








                    data=String.valueOf(algoerroneo)+" "+String.valueOf(kinoganador)+" "+String.valueOf(rekinoganador)+ "\n"+objetoslist;
                    results.setText(data);

                }
            }, new Response.ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError error) {
                    // TODO Auto-generated method stub
                    text8.setText("shit2");
                }
            });

    // Access the RequestQueue through your singleton class.
    MySingleton.getInstance(MainActivity.this).addToRequestQueue(jsObjRequest);

I hope it will help your problem!

Otherwise, you can use Retrofit, it is easily to use with JSON and HTTP!

To avoid key and object related mistakes, you can use POJO and gson First, create your POJO of your response from this site

then cast your response with this object using gson library

Gson gson = new Gson();
YourPojoClass yourPojoClassObject = gson.fromJson(webresponse, YourPojoClass.class);

Now you can get values easily from yourPojoClassObject

The problem is in the first line where you're getting the object JSONObject object= response.getJSONObject("object");

This tries to find the JSONObject by the name of "object", however, you don't have any in your JSON. So, use JSONObject "response" parameter for referencing to the objects, strings or boolean.

For example like this:

JSONObject kino = response.getJSONObject("kino");

0 Comment

NO COMMENTS

LEAVE A REPLY

Captcha image