我正在尝试在 Java 程序中解析以下 JSON。
JSON
{
"data": {
"pageIDentifier":" nametitle",
"page": {
"pageID”:” sports_league_member",
"platform":" www",
"activityType":" ent",
"businessUnit":" ent",
"productLOB":" ent",
"productOffered":" abc",
"productQualifier”:” abc:a bc”,
"flowType”:” sports_com”,
"pageDesc”:” desc”,
"attributes": {
"pageType":" www",
"host”:” finaluser”,
"appId”:” SportsAppID_user”,
"daEnvironment”:” releaser”,
"jvm":" ent_logon_01",
"xCKey":" SportsAppID_user",
"daUID":" jddc9yu5pi1yy6",
"sysEnv”:” user”,
"uri”:” /www/sportscenter”,
"daPageName":" "
}
}
}
}
JAVA程序
URL url = getClass().getResource("test.json");
File file = new File(url.getPath());
String jsonData = readFile(file.getAbsolutePath());
JSONObject jobj = new JSONObject(jsonData);
JSONArray jarr = new JSONArray(jobj.getJSONArray("data").toString());
System.out.println("jarr: " + jarr);
执行此代码时出现以下错误。请注意,jsonData 给出了整个 json 字符串,没有任何问题。
org.json.JSONException: JSONObject["data"] is not a JSONArray.
如何从上面的 json 值中解析“data”值?请指教。
最佳答案
数据
不是数组
试试这个:
System.out.println("jarr: " + jobj.getJSONObject("data").toString());
关于java - 解析 JSON 失败,因为不是 JSONArray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49544722/