simple.Jsonarray for json.Jsonarray

Asked

Viewed 44 times

0

I have a problem. In the same code, I kind of need to use both simple.Jsonarray and json.JSONArray. I can’t import both, so I’m trying to use the simple. It turns out that when I do a request, the answer is a json that java says is simple, so I can’t use the other.

In the excerpt of the code I’m having problems, I’m reading a file. It comes back as an Object, which I need to turn into json. follows the excerpt:

JSONParser parser = new JSONParser();
JSONArray queues = (JSONArray) parser.parse(new FileReader("file.json"))

The following error appears (in the first line of the above section):

java.lang.ClassCastException: org.json.simple.JSONArray cannot be cast to org.json.JSONArray

If I use the simple of the problem elsewhere in the code. I am using mvc, so it is difficult to create a new file just to make a method...

1 answer

1


You can import and use both yes. Just import normally and, when using the code, pass the complete reference, including the package path. Something like:

org.json.simple.JSONArray json = new org.json.simple.JSONArray();

If, in another code location, you need to use the other Jsonarray class, you just need to pass the full reference. For example:

org.json.JSONArray json2 = new org.json.JSONArray();

As for your current code, there is nothing to do. The casting error is quite clear.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.