4

I'm getting a JSON array in string like

[ { "id":"ca.Primary_Diagnosis_Dt",
    "field":"ca.Primary_Diagnosis_Dt",
    "type":"date",
    "input":"text",
    "operator":"not_equal",
    "value":"2016/06/07"
  },
  { "id":"ca.Clinical_Stage",
    "field":"ca.Clinical_Stage",
    "type":"integer",
    "input":"select",
    "operator":"equal",
    "value":"I"
  }
]

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

5
  • What are you using to parse the JSON? Commented Jun 27, 2016 at 11:10
  • rules : JSON.stringify(json.rules) Commented Jun 27, 2016 at 11:10
  • this i used in ajax call Commented Jun 27, 2016 at 11:11
  • So you are using JavaScript and not Java? Commented Jun 27, 2016 at 11:11
  • from ajaxcall i send this to servlet and from getter i got above value as string Commented Jun 27, 2016 at 11:13

4 Answers 4

1

Online : Working code

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();

        data.id = jsonObj.getString("id");
        data.operator = jsonObj.getString("operator");
        data.value = jsonObj.getString("value");

        dataList.add(data);
    }

Now dataList has your data!

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

JAR : http://www.java2s.com/Code/Jar/j/Downloadjavajsonjar.htm

Sign up to request clarification or add additional context in comments.

Comments

1
  1. Use any JSON parsor eg: GSON to create an arraylist of this particular json
  2. Iterate the arraylist
  3. Save it :)

Comments

0

You can do that with a JSon library such as org.glassfish.javax.json

In Maven use

    <dependency>
        <groupId>org.glassfish</groupId>
        <artifactId>javax.json</artifactId>
        <version>1.0.4</version>
    </dependency>

Then read the string to a JsonArray:

JsonArray ja = Json.createReader(new StringReader(input)).readArray();

Then iterate over the list, map the objects to a new objects with just the values you need:

List<JsonObject> list = ja.stream()
                          .map(o -> (JsonObject) o)
                          .map(jo -> Json.createObjectBuilder()
                                         .add("id", jo.getJsonString("id"))
                                         .add("operator", jo.getJsonString("operator"))
                                         .add("value", jo.getJsonString("value")).build())
                          .collect(Collectors.toList());

In case you could not use JsonObject as output, you may use a value object instead - just like Himanshu Tyagi proposed - and map the values to that object.

Comments

0

In your case, I think, the JSON should be parsed into a List<Map<String,String>>. This can be done using Jackson. Following is a method that converts a JSON string into List<Map<String,String>>:

public static List<Map<String, String>> toList(String json) throws IOException {

    List<Map<String, String>> listObj;

    ObjectMapper MAPPER = new ObjectMapper();

    listObj = MAPPER.readValue(json, new TypeReference<ArrayList<HashMap<String, String>>>() {
    });

    return listObj;
}

If you have created a POJO class for it then you can use:

public static List<PojoClass> toList(String json) throws IOException {

    List<PojoClass> listObj;

    ObjectMapper MAPPER = new ObjectMapper();

    listObj = MAPPER.readValue(json, new TypeReference<ArrayList<PojoClass>>() {
    });

    return listObj;
}

Maven dependency for Jackson:

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.6.3</version>
</dependency>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.