Parsing JSON maps / dictionaries with Gson?

前端 未结 2 2214
时光说笑
时光说笑 2021-02-19 11:21

I need to parse a JSON Response that looks like:

{\"key1\": \"value1\", 
 \"key2\": \"value2\", 
 \"key3\": 
    {\"childKey1\": \"childValue1\", 
     \"childKe         


        
2条回答
  •  忘了有多久
    2021-02-19 12:06

    As far as I remember you should create separate class for each json object. Try something like this:

    class Key { 
        @SerializedName("childKey1")
        private String mchildKey1;
    
        @SerializedName("childKey2")
        private String mchildKey2;
    
        @SerializedName("childKey3")
        private String mchildKey3;
    }
    
    class Egg { 
        @SerializedName("key1")
        private String mKey1;
    
        @SerializedName("key2")
        private String mKey2;
    
        @SerializedName("key3")
        private Key mKey3;
    }
    

    If this is not what you expected you can write your own serialize/deserialize adapter.

提交回复
热议问题