How to convert Jsonarray to Arraylist<> on android

Asked

Viewed 311 times

0

Hello, all right?

I would like your help to be able to convert a Jsonarray to Arraylist<> on android.

I make a request that returns me the following Json:

{"normal":{"test_graph":{"VALORES (R$)":"valor"},"randons":[],"id":"","valueField":null,"titleField":null,"dataProvider":[{"category":"Entrada","valor":30.00},{"category":"Comissão cambista","valor":3.00},{"category":"Total","valor":27.00}],"colors":["#002240"],"itpArrayBlue":["#002240","#0059A8","#639CFF","#76C1ED","#9AA9AD","#001323","#00498A","#8DB7FF","#619EC2","#8C9A9E","#5C768B","#8BACC9","#B8D2FF","#C5DBE8","#CAD1D2"],"itpArrayGreen":["#46CC3D","#279300","#28682B","#344F2D","#87AD87"],"graph":[{"title":"VALORES (R$)","value":"valor","lineColor":"#002240","column":true}],"oneColumn":null,"valueTime":null,"category":null,"valueAxes":[],"especial":false},"aovivo":{"test_graph":{"VALORES (R$)":"valor"},"randons":[],"id":"","valueField":null,"titleField":null,"dataProvider":[{"category":"Total","valor":0.0}],"colors":["#002240"],"itpArrayBlue":["#002240","#0059A8","#639CFF","#76C1ED","#9AA9AD","#001323","#00498A","#8DB7FF","#619EC2","#8C9A9E","#5C768B","#8BACC9","#B8D2FF","#C5DBE8","#CAD1D2"],"itpArrayGreen":["#46CC3D","#279300","#28682B","#344F2D","#87AD87"],"graph":[{"title":"VALORES (R$)","value":"valor","lineColor":"#002240","column":true}],"oneColumn":null,"valueTime":null,"category":null,"valueAxes":[],"especial":false}}v

I’ve already created the models:

Auxdata

public class AuxData {

public JsonObject normal;
public JsonObject aovivo;

public ArrayList<DataProvider> dataProvider;


public AuxData(){
    normal =  new JsonObject();
    aovivo = new JsonObject();

    dataProvider = new ArrayList<DataProvider>();
  }

}

Dataprovider

public class DataProvider {

  public String category;
  public double valor;
}

Response

private Callback<AuxData> getCaixa() {
    return new Callback<AuxData>() {
        @Override
        public void onResponse(Call<AuxData> call, Response<AuxData> response) {
            switch (response.code())
            {
                case 200:{

                    JsonObject object = response.body().normal;

                    JsonArray array = new JsonArray();

                    array = object.get("dataProvider").getAsJsonArray();

                    ArrayList<DataProvider> dataa = new ArrayList<DataProvider>();


                    break;
                }
                case 400:{

                    Toasty.error(caixadata.this, "Erro", Toast.LENGTH_LONG, true).show();

                    break;
                }
            }


        }

        @Override
        public void onFailure(Call<AuxData> call, Throwable t) {

            Toasty.error(caixadata.this, "Erro", Toast.LENGTH_LONG, true).show();
        }
    };
}

How do I fetch data from this array and save to an Arraylist? Or Convert Jsonarray to Arraylist.

Thank you!

1 answer

0

You can go through the Jsonarray and keep adding the items to your Arraylist

    ArrayList<DataProvider> data = new ArrayList<DataProvider>();
    JSONArray array = new JSONArray();
    array = object.get("dataProvider").getAsJsonArray();

    for(int i = 0; i < array.length(); i++){
        try {

            //Aqui você pega o JSONObject e pega as chaves de acordo com o que quer.
            //Exemplo:
            JSONObject json = array.getJSONObject(i);

            data.add(new DataProvider(json.getString("category"),
                                        js.getDouble("valor")));



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

IMPORTANT

Its object Dataprovider needs the getters and setters and a builder, if not, it can’t be an object.

Would look like this:

public class DataProvider {
    public String category;
    public double valor;

    public DataProvider(String category, double valor) {
        this.category = category;
        this.valor = valor;
    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public double getValor() {
        return valor;
    }

    public void setValor(double valor) {
        this.valor = valor;
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.