user6773905 user6773905 - 10 months ago 51
JSON Question

how to convert this json String to normal java arraylist using gson

//Convert this below Json to Java ArrayList

[{"file_name":"1.xml","file_ext":"application/octet-stream","sr_no":"0.1","status":"Checked ","rev":"1","locking":"0"},{"file_name":"2.xml","file_ext":"json/octet-stream","sr_no":"0.2","status":"Not Checked ","rev":"2","locking":"1"},{"file_name":"3.xml","file_ext":"application/json-stream","sr_no":"0.3","status":"Checked ","rev":"1","locking":"3"},{"file_name":"4.xml","file_ext":"application/octet-stream","sr_no":"0.4","status":"Checked ","rev":"0.4","locking":"4"}]

//Code i Used

JSONArray nameArray = (JSONArray) JSONSerializer.toJSON(output);
for(Object js : nameArray){
JSONObject json = (JSONObject) js;
System.out.println("File_Name :" +json.get("file_name"));

Answer Source

You can create a java class with entities are: file_name, file_ext, sr_no, status, rev, locking in string type.

 public class TestJson {

        private String file_name, file_ext, sr_no, status, rev, locking;

        //get & set

Then you call:

public static void main(String[] args) {
    String json = your json string;
    TestJson[] respone = new Gson().fromJson(json, TestJson[].class);
    for (TestJson s : respone) {
       System.out.println("File name: " + s.getFile_name());

So, you have a list of object you want.