How to convert JsonArray to ArrayList in android

0

Hello, how are you?

I would like your help to be able to convert a JsonArray to ArrayList < > on android.

I make a request that returns 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 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 get data from this array and save it to an ArrayList? Or convert JsonArray to ArrayList.

Thank you!

    
asked by anonymous 21.10.2018 / 03:45

1 answer

0

You can go through JsonArray and add 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

Your DataProvider object needs getters and setters and a constructor , if not, it can not be an object.

It 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;
    }
}
    
21.10.2018 / 07:20