How to handle Dynamic JSON in Retrofit?

后端 未结 10 2353
余生分开走
余生分开走 2020-11-22 16:23

I am using the retrofit efficient networking library, but I am unable to handle Dynamic JSON which contains single prefix responseMessage which changes to

10条回答
  •  爱一瞬间的悲伤
    2020-11-22 17:09

    RestClient.java

    import retrofit.client.Response;
    public interface RestClient {
      @GET("/api/foo") Response getYourJson();
    }
    

    YourClass.java

    RestClient restClient;
    
    // create your restClient
    
    Response response = restClient.getYourJson();
    
    Gson gson = new Gson();
    String json = response.getBody().toString();
    if (checkResponseMessage(json)) {
      Pojo1 pojo1 = gson.fromJson(json, Pojo1.class);
    } else {
      Pojo2 pojo2 = gson.fromJson(json, Pojo2.class);
    }
    

    You must implement "checkResponseMessage" method.

提交回复
热议问题