Make GSON accept single objects where it expects arrays

后端 未结 4 676
你的背包
你的背包 2020-12-06 06:29

I have bunch of model classes which have fields of type List where X is one of many things (e.g. String, Integer

4条回答
  •  失恋的感觉
    2020-12-06 06:56

    You can simply write your own JsonDeserializer where you check whether your bleh or foo are JsonObjects or JsonArrays.

    To check if a JsonElement is an array or an object:

    JsonElement element = ...;
    if (element.isJsonObject()) {
        //element is a JsonObject
    } else if (element.isJsonArray()) {
        //element is a JsonArray
    }
    

提交回复
热议问题