我尝试从我的Web API中解析JSON格式的文本到Rank.class的ArrayList中,但我得到了一个ClassCastException错误消息
无法将com.google.gson.JsonPrimitive转换为com.google.gson.JsonObject
下面是我的一些代码:
# RankHandler.refresh()
this.ranks = new Gson().fromJson(Juke.INSTANCE.getApiHandler().request("/ranks").getJsonResponse(), new TypeToken<List<Rank>>() {}.getType());
# Rank.class
@Getter @AllArgsConstructor
public class Rank {
private String id, displayName, prefix, inheritFromId;
private ChatColor color;
private List<String> permissions;
private int weight;
private boolean staff;
}
# Exception:
[19:11:59 WARN]: java.lang.ClassCastException: Cannot cast com.google.gson.JsonPrimitive to com.google.gson.JsonObject
[19:11:59 WARN]: at java.lang.Class.cast(Unknown Source)
[19:11:59 WARN]: at com.google.gson.Gson.fromJson(Gson.java:690)
[19:11:59 WARN]: at me.ma1de.juke.shared.api.APIRequest.getJsonResponse(APIRequest.java:54)
[19:11:59 WARN]: at me.ma1de.juke.shared.api.APIHandler.request(APIHandler.java:27)
[19:11:59 WARN]: at me.ma1de.juke.shared.api.APIHandler.request(APIHandler.java:52)
[19:11:59 WARN]: at me.ma1de.juke.shared.rank.RankHandler.refresh(RankHandler.java:44)
[19:11:59 WARN]: at org.bukkit.craftbukkit.v1_8_R3.scheduler.CraftTask.run(CraftTask.java:71)
[19:11:59 WARN]: at org.bukkit.craftbukkit.v1_8_R3.scheduler.CraftAsyncTask.run(CraftAsyncTask.java:53)
[19:11:59 WARN]: at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
[19:11:59 WARN]: at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
[19:11:59 WARN]: at java.lang.Thread.run(Unknown Source)
[19:11:59 WARN]: Exception in thread "Craft Scheduler Thread - 48"
[19:11:59 WARN]: org.apache.commons.lang.UnhandledException: Plugin juke-bukkit v1.0-SNAPSHOT generated an exception while executing task 2
at org.bukkit.craftbukkit.v1_8_R3.scheduler.CraftAsyncTask.run(CraftAsyncTask.java:56)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException
at me.ma1de.juke.shared.rank.RankHandler.refresh(RankHandler.java:44)
at org.bukkit.craftbukkit.v1_8_R3.scheduler.CraftTask.run(CraftTask.java:71)
at org.bukkit.craftbukkit.v1_8_R3.scheduler.CraftAsyncTask.run(CraftAsyncTask.java:53)
... 3 more
# What I am trying to parse as an array list of Rank.class
[{"id":"default","displayName":"Default","prefix":"","color":"WHITE","permissions":[],"weight":0,"staff":false}]
# Dependencies
<dependencies>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.8.8-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.26</version>
</dependency>
<dependency>
<groupId>me.ma1de.juke.shared</groupId>
<artifactId>Shared</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.6</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>31.1-jre</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.10.0</version>
</dependency>
</dependencies>
1条答案
按热度按时间qv7cva1a1#
首先,您提供的json字符串不包含反序列化可能需要的
inheritFromId
值。这个问题也可能是由
color
字段引起的,它被写为ChatColor
类型的对象,而在json中被表示为String
。