ANucool Mittal ANucool Mittal - 1 year ago 44
JSON Question

how to put json string into list in java

I'm getting a

JSON array
in string like

[ { "id":"ca.Primary_Diagnosis_Dt",
{ "id":"ca.Clinical_Stage",

i just want to save the value of id ,operator and value in
please help

Answer Source

First create a class to store your values :

class Data{
  String id;
  String operator;
  String value;

Then iterate over the json :

    JSONArray jsonArr = new JSONArray("[JSON Stirng]");
    List<Data> dataList = new ArrayList<>();
    for (int i = 0; i < jsonArr.length(); i++) {

        JSONObject jsonObj = jsonArr.getJSONObject(i);
        Data data = new Data(); = jsonObj.getString("id");
        data.operator = jsonObj.getString("operator");
        data.value = jsonObj.getString("value");


Now dataList has your data!

P.S. : Use getter/setters in Data class