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.
import org.json.JSONArray;such asgetStringetc. 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 theJSON FILEi 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.org.json.simpleandorg.jsonare just two different JSON libraries for Java, there is nothing that makesorg.jsonmore "normal" thanorg.json.simple. If you need to call code that needs the data asorg.jsonclasses, you should stick with that, and parse the JSON file withorg.json, notorg.json.simple. Mixing two different (competing) JSON libraries is just weird, and entirely unnecessary.