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 ?

Programmer Bruce
  • 64,977
  • 7
  • 99
  • 97
chrithccmc
  • 1,069
  • 4
  • 14
  • 17

3 Answers3

7

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

WhiteFang34
  • 70,765
  • 18
  • 106
  • 111
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 :)

Jubayer Arefin
  • 485
  • 7
  • 17
0

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

David Sowsy
  • 1,680
  • 14
  • 13