How to handle Dynamic JSON in Retrofit?

后端 未结 10 2228
余生分开走
余生分开走 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 16:51

    I know I am very very late to the party. I had a similar issue and just solved it like this:

    public class TrackerRefResponse {
    
        private String applicationType;
        // Changed to Object. Works fine with String and array responses.
        private Object responseMessage;
    
    }
    

    I literally just changed to type to Object. I chose this approach because only one field in the response was dynamic (for me, my response was way more complicated), so using a converter would have made life difficult. Used Gson to work with the Object from there, depending on if it was a String or Array value.

    Hope this helps someone looking for a simple answer :).

提交回复
热议问题