0
 import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
String Json = {"AccountToken":{"string":"hello"},"Event":{"string":"t"}}
JSONObject genreJsonObject =(JSONObject)JSONValue.parseWithException(json);
String account_id = (String) genreJsonObject.get("AccountToken");

Throws java.lang.ClassCastException error

What could be wrong please help?

3
  • for instance the fact that the content of AccountToken is not a String ? Commented Feb 11, 2015 at 21:22
  • if AccountToken is also jsonObject, how will code change? Commented Feb 11, 2015 at 21:31
  • AccountToken is not a string. Commented Feb 11, 2015 at 21:48

1 Answer 1

1

AccountToken is an JSON object, not a String...

You'll need to cast it to JSONObject and call get() on it again to get a value from its internal structure

String json = "{\"AccountToken\":{\"string\":\"hello\"},\"Event\":{\"string\":\"t\"}}";
JSONObject genreJsonObject =(JSONObject)JSONValue.parseWithException(json);
JSONObject accountToken = (JSONObject) genreJsonObject.get("AccountToken");
System.out.println(accountToken.get("string"));

==> hello
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.