Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

json

I tried to convert following JSON string into Array and got following error:

Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory at net.sf.json.AbstractJSON.(AbstractJSON.java:54) at net.sf.json.util.CycleDetectionStrategy.(CycleDetect‌​ionStrategy.java:36) at net.sf.json.JsonConfig.(JsonConfig.java:65) at net.sf.json.JSONSerializer.toJSON(JSONSerializer.java:84)

JSON:

[  
   {  
      "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:

JSONArray nameArray = (JSONArray) JSONSerializer.toJSON(output);
System.out.println(nameArray.size());
for(Object js : nameArray)
{
    JSONObject json = (JSONObject) js;
    System.out.println("File_Name :" +json.get("file_name"));                                         
}
like image 319
user6773905 Avatar asked Sep 23 '16 09:09

user6773905


2 Answers

I know the question is about converting JSON String to Java Array, but I would like to also answer about how to convert the JSON String to an ArrayList using the Gson Library.

Since I spend a good amount of time in solving this, I hope my solution may help others.

My JSON string looks similar to this one -

enter image description here

I had an object named StockHistory, and I wanted to convert this JSON into an ArrayList of StockHistory.

This is how my StockHistory class looked -

class StockHistory {    
 Date date;
 Double open;
 Double high;
 Double low;
 Double close;
 Double adjClose;
 Double volume;
}

The code that I used to convert the JSON Array to the ArrayList of StockHistory is as follows -

Gson gson = new Gson();
Type listType = new TypeToken< ArrayList<StockHistory> >(){}.getType();
List<StockHistory> history = gson.fromJson(reader, listType);

Now if you are reading your JSON from a file, the reader's initialization would be -

Reader reader = new FileReader(fileName);

and if you are just converting a string to JSON object then, the reader's initialization would simply be -

String reader = "{ // json String }";

Hope that helps. Cheers!!!

like image 144
Rito Avatar answered Oct 22 '22 12:10

Rito


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.

like image 38
Numb Avatar answered Oct 22 '22 11:10

Numb