[英]Deserialize Json with same structure but different field names
我有以下兩個Jsons
"attrs": {
"rect": {
"fill": "blue"
},
"text": {
"fill": "white",
"text": "my box"
}
}
和
"attrs": {
"circle": {
"fill": "red"
},
"text": {
"text": "my circle",
"fill": "yellow"
}
}
有沒有一種方法可以在解析時反序列化JSON到特定的類,即class Rect
和class Circle
? 我正在使用Gson 2.4
這就是我現在所說的:
Element[] element = gson.fromJson(arguments.toJson(), Element[].class);
而Element類具有這種結構
public class Element {
private Attributes attr;
public class Attributes{
private Rectangle rect;
private Circle circle;
}
如何告訴gson選擇其中一個字段? 現在,其中之一將為空,我不希望那樣,因為我將擁有更多的形狀。
如果“ circle”或“ rect”始終是第一個孩子,則可以編寫簡單的if else
邏輯來確定要反序列化的類。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.