使用 gson 解析 JSON 数组中的多种类型
Parsing multiple types in a JSON array with gson
我正在尝试使用带有 gson 的 POJO 来解析 JSON 字符串,但在尝试读取以下 JSON 数组时,我遇到了 运行 问题:
{"translate":"chat.type.text","with":[{"insertion":"woder22","clickEvent":{"action":"suggest_command","value":"/msg woder22 "},"hoverEvent":{"action":"show_entity","value":"{name:\"woder22\",id:\"bbd02ce0-24de-4683-8c8f-5d7e6b7dffa6\",}"},"text":"woder22"},"hi"]}
一切正常,直到我到达 "with" 部分,我正在尝试使用以下 POJO
来解析它
public class ChatMessage {
private String text = "";
private String translate;
private List<With> with = new ArrayList<With>();
private String score;
private String selector;
private List<Node> extra;
private String bold = "false";
private String italic = "false";
private String underlined = "false";
private String strikethrough = "false";
private String obfuscated = "false";
private String color;
private Clicked clickEvent;
private Hover hoverEvent;
private String insertion;
//getter and setter method here
}
class Node {
private String color;
private String text;
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
class Clicked {
private String action;
private String value;
public String getAction() {
return action;
}
public void setAction(String action) {
this.action = action;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
class Hover {
private String action;
private String value;
public String getAction() {
return action;
}
public void setAction(String action) {
this.action = action;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
我已将其更改为显示所有代码
public class With {
private String translate;
private Clicked clickEvent;
private Hover hoverEvent;
private String insertion;
private String text = "";
//setter and getters
public ChatMessage getNonNull(ChatMessage mes){
if(this.text != null)mes.setText(this.text);
if(this.translate != null)mes.setTranslate(this.translate);
if(this.score != null)mes.setScore(this.score);
if(this.selector != null)mes.setSelector(this.selector);
if(this.extra != null)mes.setExtra(this.extra);
if(this.bold != null)mes.setBold(this.bold);
if(this.italic != null)mes.setItalic(this.italic);
if(this.underlined != null)mes.setUnderlined(this.underlined);
if(this.strikethrough != null)mes.setStrikethrough(this.strikethrough);
if(this.obfuscated != null)mes.setObfuscated(this.obfuscated);
if(this.color != null)mes.setColor(this.color);
if(this.clickEvent != null)mes.setClickEvent(this.clickEvent);
if(this.hoverEvent != null)mes.setHoverEvent(this.hoverEvent);
if(this.insertion != null)mes.setInsertion(this.insertion);
return mes;
}
}
现在的问题是,当 Gson 尝试解析它时,它当然 运行 变成了 "with" 数组的第二部分不是 With 对象的问题。我的问题是我不知道如何处理这个问题。任何帮助将不胜感激。
编辑1
它应该做什么:
with 数组只是假设是一种 "overwrite" ,因为主字符串中的任何字段都可以被覆盖并在内部单独格式化。这就是 With class 底部的 null 东西应该做的,它应该用覆盖的内容编辑主要变量。未命名的字段应该是这里的文本变量。
以下是为此编写自定义反序列化程序的方法:
class ChatMessageDezerializer implements JsonDeserializer<ChatMessage> {
@Override
public ChatMessage deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
ChatMessage message = new ChatMessage();
JsonObject obj = json.getAsJsonObject();
message.translate = obj.get("translate").getAsString();
JsonArray array = obj.getAsJsonArray("with");
message.with.add(context.deserialize(array.get(0), With.class));
message.with.add(array.get(1).getAsString());
return message;
}
}
并将其注册到您的 Gson 解析器中:
Gson gson = new GsonBuilder().registerTypeAdapter(ChatMessage.class, new ChatMessageDezerializer()).create();
请注意,with
现在是 List<Object>
,因为数组中元素最具体的通用类型是 Object。另外我只是完成了有问题的部分,其余的都可以轻松处理。 运行 这就是你的结局
[With@b1a58a3, hi]
作为结果列表。这假设您对返回的结构有最低限度的控制(或者至少您知道它的格式)。它应该给你一个很好的起点。
我正在尝试使用带有 gson 的 POJO 来解析 JSON 字符串,但在尝试读取以下 JSON 数组时,我遇到了 运行 问题:
{"translate":"chat.type.text","with":[{"insertion":"woder22","clickEvent":{"action":"suggest_command","value":"/msg woder22 "},"hoverEvent":{"action":"show_entity","value":"{name:\"woder22\",id:\"bbd02ce0-24de-4683-8c8f-5d7e6b7dffa6\",}"},"text":"woder22"},"hi"]}
一切正常,直到我到达 "with" 部分,我正在尝试使用以下 POJO
来解析它public class ChatMessage {
private String text = "";
private String translate;
private List<With> with = new ArrayList<With>();
private String score;
private String selector;
private List<Node> extra;
private String bold = "false";
private String italic = "false";
private String underlined = "false";
private String strikethrough = "false";
private String obfuscated = "false";
private String color;
private Clicked clickEvent;
private Hover hoverEvent;
private String insertion;
//getter and setter method here
}
class Node {
private String color;
private String text;
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
class Clicked {
private String action;
private String value;
public String getAction() {
return action;
}
public void setAction(String action) {
this.action = action;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
class Hover {
private String action;
private String value;
public String getAction() {
return action;
}
public void setAction(String action) {
this.action = action;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
我已将其更改为显示所有代码
public class With {
private String translate;
private Clicked clickEvent;
private Hover hoverEvent;
private String insertion;
private String text = "";
//setter and getters
public ChatMessage getNonNull(ChatMessage mes){
if(this.text != null)mes.setText(this.text);
if(this.translate != null)mes.setTranslate(this.translate);
if(this.score != null)mes.setScore(this.score);
if(this.selector != null)mes.setSelector(this.selector);
if(this.extra != null)mes.setExtra(this.extra);
if(this.bold != null)mes.setBold(this.bold);
if(this.italic != null)mes.setItalic(this.italic);
if(this.underlined != null)mes.setUnderlined(this.underlined);
if(this.strikethrough != null)mes.setStrikethrough(this.strikethrough);
if(this.obfuscated != null)mes.setObfuscated(this.obfuscated);
if(this.color != null)mes.setColor(this.color);
if(this.clickEvent != null)mes.setClickEvent(this.clickEvent);
if(this.hoverEvent != null)mes.setHoverEvent(this.hoverEvent);
if(this.insertion != null)mes.setInsertion(this.insertion);
return mes;
}
}
现在的问题是,当 Gson 尝试解析它时,它当然 运行 变成了 "with" 数组的第二部分不是 With 对象的问题。我的问题是我不知道如何处理这个问题。任何帮助将不胜感激。
编辑1 它应该做什么: with 数组只是假设是一种 "overwrite" ,因为主字符串中的任何字段都可以被覆盖并在内部单独格式化。这就是 With class 底部的 null 东西应该做的,它应该用覆盖的内容编辑主要变量。未命名的字段应该是这里的文本变量。
以下是为此编写自定义反序列化程序的方法:
class ChatMessageDezerializer implements JsonDeserializer<ChatMessage> {
@Override
public ChatMessage deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
ChatMessage message = new ChatMessage();
JsonObject obj = json.getAsJsonObject();
message.translate = obj.get("translate").getAsString();
JsonArray array = obj.getAsJsonArray("with");
message.with.add(context.deserialize(array.get(0), With.class));
message.with.add(array.get(1).getAsString());
return message;
}
}
并将其注册到您的 Gson 解析器中:
Gson gson = new GsonBuilder().registerTypeAdapter(ChatMessage.class, new ChatMessageDezerializer()).create();
请注意,with
现在是 List<Object>
,因为数组中元素最具体的通用类型是 Object。另外我只是完成了有问题的部分,其余的都可以轻松处理。 运行 这就是你的结局
[With@b1a58a3, hi]
作为结果列表。这假设您对返回的结构有最低限度的控制(或者至少您知道它的格式)。它应该给你一个很好的起点。