user2942227
user2942227

Reputation: 1023

Json parsing using Simple json library

 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

Answers (1)

Adam
Adam

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

Related Questions