簡體   English   中英

使用GSON將具有不同名稱的字段反序列化為同一字段

[英]Deserializing fields with different names to the same field with GSON

我正在努力通過本地化反序列化World Weather Online API結果。

根據給定語言而以lang-{locale}塊形式返回的本地化數據(WWO支持約40種語言:

 "hourly": [

           {

              "chanceoffog": "0",

              "chanceoffrost": "0",

              "chanceofhightemp": "0",

              "chanceofovercast": "63",

              "chanceofrain": "3",

              "chanceofremdry": "0",

              "chanceofsnow": "0",

              "chanceofsunshine": "0",

              "chanceofthunder": "0",

              "chanceofwindy": "0",

              "cloudcover": "88",

              "DewPointC": "1",

              "DewPointF": "34",

              "FeelsLikeC": "4",

              "FeelsLikeF": "39",

              "HeatIndexC": "7",

              "HeatIndexF": "44",

              "humidity": "67",

              "lang_ru": [

                 {

                    "value": "Пасмурно"

                 }

              ],

              "precipMM": "0.0",

              "pressure": "1033",

              "tempC": "7",

              "tempF": "44",

              "time": "24",

              "visibility": "10",

              "weatherCode": "122",

              "weatherDesc": [

                 {

                    "value": "Overcast"

                 }

              ],

              "weatherIconUrl": [

                 {

                    "value": "http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0004_black_low_cloud.png"

                 }

              ],

              "WindChillC": "4",

              "WindChillF": "39",

              "winddir16Point": "WNW",

              "winddirDegree": "294",

              "WindGustKmph": "17",

              "WindGustMiles": "11",

              "windspeedKmph": "14",

              "windspeedMiles": "9"

           }

        ]

也可以是lang_eslang_fr等。

無論給定哪種語言,我都需要使用字符串將其反序列化為一個字段。

我想到的一個難看的解決方案是根據語言添加40個字段,並檢查相關字段是否不為null。

另一個非常丑陋的解決方案是每小時編寫一個完整的自定義解串器,但是每小時每小時有(超過30個)簡單字符串字段的負載,因此編寫自定義解串器似乎效率低下並且與GSON的簡單性背道而馳。

有沒有更好,更簡單的解決方案來編寫僅用於lang對象的自定義反序列化器,並允許GSON自動反序列化其余數據?

我有一個類似的問題,即用戶對象可以具有id,userId或user_id,這就是我設法將它們全部三個映射到id的方式。

public class UserDeserializer implements JsonDeserializer<User> {


@Override
public User deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {

    try {
        User user = new GsonBuilder().create().fromJson(json, User.class);
        if (user.id == null) {
            JsonObject jsonObject = json.getAsJsonObject();
            JsonElement userId = jsonObject.get("userid");

            if (userId != null) {
                user.id = userId.getAsString();
                return user;
            }
            JsonElement user_id = jsonObject.get("user_id");
            if (user_id != null) {
                user.id = user_id.getAsString();
                return user;
            }
        }
        return user;
    } catch (JsonParseException ex) {
        ex.printStackTrace();
        throw ex;
    }
}

}

暫無
暫無

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

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