java - 解析 JSON 失败,因为不是 JSONArray

标签 java json

我正在尝试在 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/

相关文章:

java - 单元测试和白盒测试

java - 通过两个参数java过滤对象

javascript - 读取JQuery中的Json字段

iphone - 在 cocoa 中使用 JSON

java - 为什么我们需要 war ?如果我们可以创建 JAR 并将其部署到 servlet 容器上

java - 为什么我不能让我的程序一遍又一遍地运行我的方法(while 循环)?

C# - 无法反序列化当前的 JSON 数组

javascript - 如何分离 JSON.stringify 结果

xml - Jquery .find 适用于 xml 但不适用于 JSON

java - 由 : There is no result type defined for type 'chain' mapped with name 'success' 引起