我試圖解析使用與GSON一個POJO JSON字符串,我遇到了試圖讀取以下JSON陣列然而,當一個障礙:解析多種類型的JSON陣列GSON
{"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"]}
一切直到我到了「有」的一部分,我想通過以下的POJOs
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類底部的null事物是假設要做的事情,它假定用它們被覆蓋的內容編輯主要變量。未命名的字段在這裏假定爲文本變量。
你想用這個「hi」值表示什麼?將它存儲在列表中?丟棄它? – 2015-04-04 22:26:03
哦,是的 - 我試圖把它放在一個變量,在這種情況下,「文本」變量(其實際文本,而其餘的這裏是格式),如果它有幫助任何,這裏是關於實際的JSON字符串在這裏使用:http://wiki.vg/Chat – woder 2015-04-04 22:28:46
你鏈接的文檔沒有顯示這個特定的結構(與數組談論)。無論如何,你可以編寫一個自定義的反序列化器來解析這個JSON。 – 2015-04-04 22:35:32