[英]Gson custom deseralizer for one variable in an object
我的例子:
我們有一個Apple的對象類型。 Apple有一些成員變量:
String appleName; // The apples name
String appleBrand; // The apples brand
List<Seed> seeds; // A list of seeds the apple has
種子對象如下所示。
String seedName; // The seeds name
long seedSize; // The size of the seed
現在,當我得到一個蘋果對象時,一個蘋果可能有一個以上的種子,或者它可能有一顆種子,或者可能沒有種子!
帶有一個種子的示例JSON apple:
{
"apple" : {
"apple_name" : "Jimmy",
"apple_brand" : "Awesome Brand" ,
"seeds" : {"seed_name":"Loopy" , "seed_size":"14" }
}
}
示例JSON apple有兩個種子:
{
"apple" : {
"apple_name" : "Jimmy" ,
"apple_brand" : "Awesome Brand" ,
"seeds" : [
{
"seed_name" : "Loopy",
"seed_size" : "14"
},
{
"seed_name" : "Quake",
"seed_size" : "26"
}
]}
}
現在問題是第一個例子是種子的JSONObject,第二個例子是種子的JSONArray。 現在我知道它不一致的JSON,修復它的最簡單方法是修復JSON本身,但不幸的是我從其他人那里得到了JSON,所以我無法解決它。 解決這個問題最簡單的方法是什么?
您需要為Apple
類型注冊自定義類型適配器。 在類型適配器中,您將添加邏輯以確定您是否獲得了數組或單個對象。 使用該信息,您可以創建Apple
對象。
在下面的代碼中,修改Apple模型對象,以便不自動解析seeds
字段。 將變量聲明更改為:
private List<Seed> seeds_funkyName;
這是代碼:
GsonBuilder b = new GsonBuilder();
b.registerTypeAdapter(Apple.class, new JsonDeserializer<Apple>() {
@Override
public Apple deserialize(JsonElement arg0, Type arg1,
JsonDeserializationContext arg2) throws JsonParseException {
JsonObject appleObj = arg0.getAsJsonObject();
Gson g = new Gson();
// Construct an apple (this shouldn't try to parse the seeds stuff
Apple a = g.fromJson(arg0, Apple.class);
List<Seed> seeds = null;
// Check to see if we were given a list or a single seed
if (appleObj.get("seeds").isJsonArray()) {
// if it's a list, just parse that from the JSON
seeds = g.fromJson(appleObj.get("seeds"),
new TypeToken<List<Seed>>() {
}.getType());
} else {
// otherwise, parse the single seed,
// and add it to the list
Seed single = g.fromJson(appleObj.get("seeds"), Seed.class);
seeds = new ArrayList<Seed>();
seeds.add(single);
}
// set the correct seed list
a.setSeeds(seeds);
return a;
}
});
有關更多信息,請參閱Gson指南 。
我們使用數組而不是列表與GSON,並沒有這樣的問題:看看http://app.ecwid.com/api/v1/1003/product?id=4064 “類別”屬性實際上是一個Javascript數組有一個元素。 它是這樣聲明的:
類別[]類別;
更新:使用TypeToken和自定義序列化可能有所幫助,請參閱此文檔: https : //sites.google.com/site/gson/gson-user-guide
我遇到了同樣的問題。 我認為我的解決方案更簡單,更通用:
Gson gson = new GsonBuilder()
.registerTypeAdapter(List.class, new JsonSerializer<List<?>>() {
@Override
public JsonElement serialize(List<?> list, Type t,
JsonSerializationContext jsc) {
if (list.size() == 1) {
// Don't put single element lists in a json array
return new Gson().toJsonTree(list.get(0));
} else {
return new Gson().toJsonTree(list);
}
}
}).create();
當然,我同意原始海報,最好的解決方案是改變json。 大小為1的數組沒有任何問題,它會使序列化和反序列化更加簡單! 不幸的是,有時候這些變化是你無法控制的。
如果您無法更改JSON(從其他人那里獲取),那么最簡單的解決方案是在Java類中更改Apple和Seed類變量名,以便它與解析的JSON匹配。
將其更改為:
Apple Class
-----------
String apple_name; // The apples name
String apple_brand; // The apples brand
List<Seed> seeds; // A list of seeds the apple has
And the seed object looks as follows.
Seed Class
-----------
String seed_name; // The seeds name
long seed_size; // The size of the seed
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.