Json 使用简单 json 库解析
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");
抛出 java.lang.ClassCastException 错误
请问有什么问题吗?
AccountToken 是一个 JSON 对象,而不是字符串...
您需要将其转换为 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
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 错误
请问有什么问题吗?
AccountToken 是一个 JSON 对象,而不是字符串...
您需要将其转换为 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