Convert Json to an object in Android with gson

后端 未结 3 1892
一个人的身影
一个人的身影 2021-01-22 01:44

I have a JSON array with a lot of entries and want to deserialize each check into a single object with gson. My problem is to find the proper object structure for it.



        
相关标签:
3条回答
  • 2021-01-22 01:55

    Annotation needs to be added to make GSON

    eg: Add @SerializedName("client") before declaring the string client

    @SerializedName("client")
    public String client;
    

    Instead of writing code repeatedly, you can do these in one go. check this - http://www.jsonschema2pojo.org/

    0 讨论(0)
  • 2021-01-22 02:01

    You need to set the annotation like this to use GSON

    public class Client {
            @SerializedName("client")
            public String client;
    
            @SerializedName("check")
            public Check check;
        }
    
    0 讨论(0)
  • 2021-01-22 02:06

    When we check your json from http://jsonviewer.stack.hu/ we see that "subdue" is not a String Array. It's an object.

    So you need a class like:

    public class Subdue {
        public String at;
        public String begin;
        public String end;
    }
    

    and modify your Check Class like:

    public class Check {
        public Boolean handle;
        public Boolean standaloone;
        public int interval;
        public int refresh;
        public String[] dependencies;
        public String commmand;
        public int occurrences;
        public String[] subscribers;
        public Boolean aggregate;
        public Subdue subdue;
        public String name;
        public int issued;
        public int executed;
        public float duration;
        public String output;
        public int status;
    }
    

    I couldn't see any other problems. I hope this'll help you. Good Luck.

    0 讨论(0)
提交回复
热议问题