我试图使用带有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对象的问题。我的问题是我不知道如何处理这个问题。任何帮助将不胜感激。
EDIT1 这是假设: with数组简单地假设是一种“覆盖”,因为主字符串中的任何字段都可以被覆盖并在内部单独格式化。这就是With类底部的null值假设要做的事情,假设用它们被覆盖的内容编辑主变量。未命名的字段在这里被假定为文本变量。
答案 0 :(得分:4)
以下是为此编写自定义反序列化器的方法:
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]
作为结果列表。这假设你对你得到的结构有一个最小的控制(或者至少你知道它将采用哪种格式)。它应该从那里给你一个很好的起点。