Reputation: 1023
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?
Upvotes: 0
Views: 156
Reputation: 36703
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
Upvotes: 1