简体   繁体   English

用于将JSONObject转换为JSONArray的ClassCastException

[英]ClassCastException for downcast JSONObject to JSONArray

i'm using the simple JSON library to write a match log analyzer for tf2. 我正在使用简单的JSON库为tf2编写匹配日志分析器。 The code successfully gets all log IDs but cannot get to the actual log itself. 该代码成功获取了所有日志ID,但无法获取实际的日志本身。 The error is that 错误是

Exception in thread "main" java.lang.ClassCastException: org.json.simple.JSONObject cannot be cast to org.json.simple.JSONArray 线程“主”中的异常java.lang.ClassCastException:org.json.simple.JSONObject无法转换为org.json.simple.JSONArray

However, in the code, I've already casted an object to an array. 但是,在代码中,我已经将一个对象强制转换为数组。 Here is a snippet of the code, where parseJSON returns a JSONObject and logIDList contains a list of all the log ids: 以下是代码片段,其中parseJSON返回JSONObject,而logIDList包含所有日志ID的列表:

JSONArray playerData = (JSONArray)parseJSON("http://logs.tf/json_search?player=" + steamID64).get("logs");
    //....
        JSONArray tempJSONArray = (JSONArray)parseJSON("http://logs.tf/json/" + logIDList.get(j)).get("players");

The second attempt at casting the JSONObject always throws a casting error. 强制转换JSONObject的第二次尝试始终会引发强制转换错误。 Using IntelliJ's debugger, parseJSON successfully parses the JSON and returns multiple keys. 使用IntelliJ的调试器,parseJSON成功解析JSON并返回多个键。

The first JSON file is structured as so: 第一个JSON文件的结构如下:

{
 "logs": [
{
  "date": 1512093930, 
  "id": 1893064, 
  "title": "UGC 6v6 Match: RED vs -rep"
},
],
} 

The second JSON file is structured as so: 第二个JSON文件的结构如下:

{  
"players" : {
"[U:1:61383870]":{(Player Stats)}
},
}

My assumption is that it is due to there being a key within a key or something like that? 我的假设是,这是由于某个键中存在某个键之类的? Not sure why this tells me I can't cast this to an array, when I did it with another JSONObject. 不知道为什么当我使用另一个JSONObject执行此操作时,为什么不能将其转换为数组。

You are downcasting from Object to Array. 您正在从对象下放到数组。 This works if the instance really is an array, and fails if it not (like a map). 如果实例确实是数组,则此方法有效,否则,则失败(如映射)。 You should always protect downcasts with instanceof checks in general, like: 一般而言,您应该始终使用instanceof检查来保护向下转换,例如:

JSONArray playerData;
JSONObject playerJson = parseJSON("http://logs.tf/json_search?player=" + steamID64).get("logs");
if (playerJson instanceof JSONArray) {
    playerData = (JSONArray) playerJson;
} else {
    throw new IllegalStateException("wrong Json type " + playerJson)
}

As you can see from the json you posted: 从发布的json中可以看到:

"logs": [ ...]

log is an array, while 日志是一个数组,而

"players" : { ... }

players is a map. 玩家是一张地图。

As it is clearly visible that the JSONObject which you have metioned is kind of map, so you can't simply cast it to JSONArray.For this you can create your JSONArray and then proceed. 显而易见,您提到的JSONObject是一种映射,因此您不能简单地将其强制转换为JSONArray。为此,您可以创建JSONArray然后继续进行操作。 To create JSONObject to JSONArray you can use: 要将JSONObject创建为JSONArray,可以使用:

 JSONArray JSONFirewallRules = jsonObject.getJSONArray(jsonStrings.REQUEST_RULES_ALL_RESPONSE); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM