0

I am writing a JAVA program to read the JSON file data and do some processing. For reading the JSON file data from my Class path resources folder I am using the following code:

import org.json.simple.JSONArray;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

String filePath = "src/main/resources/SampleRequest.json";
JSONParser parser = new JSONParser();
Object object = parser.parse(new FileReader(filePath));
JSONArray inputjsonArr = (JSONArray) object;

As we can see I am using the import org.json.simple.JSONArray to read and get the data. Instead of that, I tried using the normal JSONArray import directly import org.json.JSONArray; then I get the error:

Exception in thread "main" java.lang.ClassCastException: org.json.simple.JSONArray cannot be cast to org.json.JSONArray

I wanted to know if it's possible to convert the simple.JSONArray to normal JSONArray. Or is it possible for me to read the data from the JSON file using the direct import org.json.JSONArray; so that I do not have to deal with this conversion at all.

4
  • 2
    Aren't those two totally different, separate libraries? Why would you convert between them? Commented Jan 11, 2021 at 10:13
  • Actually, for further processing, I would like to use the methods from import org.json.JSONArray; such as getString etc. Also, my other parts of the program is already using the org.json.JSONArray so I would like to use the same for whole thing. Only for reading the JSON FILE i found this approach and I used this. But after implementing the whole thing I realized that I need to completely use the different methods and classes to get and process the data within the JSONObject of this JSONArray so I thought if there is a way to convert then I can convert and proceed with old approach. Commented Jan 11, 2021 at 10:17
  • 2
    You should really only use one library. Even so the classes are named identical or similar they have NOTHING in common. You cannot cast between them. Basically the only way to convert between them is to convert to json and then parse the json again at which point you can just leave the first library out of the picture and only use the second to the initial parsing and manipulation or whatever you want to do with it. Commented Jan 11, 2021 at 10:22
  • 1
    "normal JSONArray" org.json.simple and org.json are just two different JSON libraries for Java, there is nothing that makes org.json more "normal" than org.json.simple. If you need to call code that needs the data as org.json classes, you should stick with that, and parse the JSON file with org.json, not org.json.simple. Mixing two different (competing) JSON libraries is just weird, and entirely unnecessary. Commented Jan 11, 2021 at 10:22

1 Answer 1

1

org.json.simple and org.json are different and incompatible libraries.

The use of org.json.simple shouldn't be recommended as it returns all of its values as an Object, whereas org.json allows to dictate what type to return.

The solution is not to use the org.json.simple.parser.JSONParser (delete all of the org.json.simple imports) but to read the String from the relevant file and pass that to the org.json.JSONArray directly, like so:

String json = FileUtils.readFileToString(new File( "src/main/resources/SampleRequest.json"), "UTF-8" );
JSONArray array = new JSONArray(json);

As you can see, I like to use the FileUtils from the following package for simplicity's sake, so add this dependency:

<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.8.0</version>
</dependency>
Sign up to request clarification or add additional context in comments.

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.