簡體   English   中英

使用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對象的問題。 我的問題是我不知道該如何處理。 任何幫助將不勝感激。

Edit1假定要執行的操作:with數組只是假定為一種“覆蓋”,因為在主字符串的任何字段中都可以覆蓋並且可以在內部單獨格式化。 那就是With類底部的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();

注意,由於數組中元素的最特定的通用類型是Object,所以with現在是List<Object> 我也只是做了有問題的部分,其余的都可以很容易地處理。 運行它最終會導致

[With@b1a58a3, hi]

作為結果列表。 假定您對返回的結構具有最小的控制(或者至少知道它的格式)。 從那里應該為您提供一個良好的起點。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM