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");
抛出java.lang.ClassCastException错误
有什么不对的请帮忙?
答案 0 :(得分:1)
AccountToken是一个JSON对象,而不是String ...
您需要将其强制转换为JSONObject并再次调用get()以从其内部结构中获取值
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