3

JSON-Simple

JSON-Simple Example

String login = "{\"result\":[104192,42068],\"id\":1}";

Object obj = JSONValue.parse(login);

JSONArray array = (JSONArray)obj;

This throw a exception

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

What is the problem in this code ?

3 Answers 3

7

In this case the parsed result is a JSONObject so you need to cast it to that.

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

1 Comment

What if the JSONObject is used as an object elsewhere? Why is the cast on assignment not enough?
0

You are trying to cast an object into an array. Try adding the object to the array instead.

Comments

0

I had this problem and I have fixed it. Here is my code snippet:

Object obj = JSONValue.parse(response.getBody());
JSONObject jsonObject = (JSONObject) obj;
Object gu_obj = jsonObject.get("guid");

I got the yahoo user guid from the request body. Hope this helps :)

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.