2015-04-04 44 views
0

我試圖解析使用與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事物是假設要做的事情,它假定用它們被覆蓋的內容編輯主要變量。未命名的字段在這裏假定爲文本變量。

+0

你想用這個「hi」值表示什麼?將它存儲在列表中?丟棄它? – 2015-04-04 22:26:03

+0

哦,是的 - 我試圖把它放在一個變量,在這種情況下,「文本」變量(其實際文本,而其餘的這裏是格式),如果它有幫助任何,這裏是關於實際的JSON字符串在這裏使用:http://wiki.vg/Chat – woder 2015-04-04 22:28:46

+0

你鏈接的文檔沒有顯示這個特定的結構(與數組談論)。無論如何,你可以編寫一個自定義的反序列化器來解析這個JSON。 – 2015-04-04 22:35:32

回答

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。此外,我只是做了有問題的部分,其餘部分可以輕鬆處理。運行這個你最終與

[[email protected], hi] 

作爲結果列表。這假設你對你回來的結構有一個最低限度的控制(或者至少你知道它將採用哪種格式)。它應該給你一個很好的起點。

+0

嘿!它光榮地運作!非常感謝你! – woder 2015-04-04 23:47:01

+0

@Alexis,你能告訴我關於泛型的一些你在代碼中實現的方式,我幾乎沒有什麼知識(比如如何在ArrayList 中使用),並且想要學習如何使用泛型 – 2015-04-06 05:11:48