GSON ignore elements with wrong type

后端 未结 5 980
陌清茗
陌清茗 2020-12-08 11:22

I\'m using Retrofit (in combination with OkHttp and GSON) to communicate with an online webservice. The webservice has a default wrapper around all it\'s responses, similar

5条回答
  •  春和景丽
    2020-12-08 12:00

    First, this is a bad API design that you're dealing with. :-(

    You can use a custom JsonDeserializer to handle this case.

    Register it with Retrofit:

    MyJsonDeserializer deserializer = new MyJsonDeserializer()).create();
    final Gson gson = new GsonBuilder().registerTypeAdapter(ApiResult.class, deserializer);
    RestAdapter restAdapter = new RestAdapter.Builder()
        .setEndpoint(API_URL)
        .setConverter(new GsonConverter(gson))
        .build();
    

提交回复
热议问题