5

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"));                                         
}
3
  • 4
    I'm voting to close this question as off-topic because this isn't even a question but a work request. Commented Sep 23, 2016 at 9:23
  • At least show your current work, and tell us what problems you're having with it. Don't post a lazy question that shows no evidence of effort Commented Sep 23, 2016 at 9:24
  • Sorry to say but this is not work request actually i have tried the answer and got exception Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory at net.sf.json.AbstractJSON.<clinit>(AbstractJSON.java:54) at net.sf.json.util.CycleDetectionStrategy.<clinit>(CycleDetectionStrategy.java:36) at net.sf.json.JsonConfig.<clinit>(JsonConfig.java:65) at net.sf.json.JSONSerializer.toJSON(JSONSerializer.java:84) Commented Sep 23, 2016 at 9:44

3 Answers 3

6

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!!!

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

Comments

2

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.

2 Comments

can u please help me with example as i have created the class jut wan to know how to get list of object through code.
i'm not really to understand your mean, can u talk your mean clearly more?
0

Firstly I have to say your question is quite "ugly" and next time please improve your question's quality.

Answer:

Try to use com.fasterxml.jackson.databind.ObjectMapper

If you have a java class to describe your items in the list:

final ObjectMapper mapper = new ObjectMapper();
YourClass[] yourClasses = mapper.readValue(YourString, YourClass[].class);

Then convert the array to a List.

If you don't have a java class, just you LinkedHashMap instead.

1 Comment

GSON also has a formJson() method

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.