如何使用gson反序列化从地图反序列化对象

时间:2016-04-14 04:55:56

标签: java json gson deserialization

我有以下Json结构,我想将Json反序列化到我自己的类中。这可以使用自定义反序列化器完成,还是必须通过遍历JsonTree手动创建对象?

输入Json

{
  "20160411": [
             {
              "name": "John",
              "provider": "Some Name",
              "published": "2016-04-11 00:00:00 -0400",
              "formats": {
                      "key1": {
                              "width": 2048,
                              "height": 651,
                              "url": "https://some.dummy.url"
                            },
                      "key2": {
                              "width": 2048,
                              "height": 651,
                              "url": "https://some.dummy.url"
                            },
                      "key3": {
                              "width": 2048,
                              "height": 651,
                              "url": "https://some.dummy.url"
                            },
                      "key4": {
                              "width": 2048,
                              "height": 651,
                              "url": "https://some.dummy.url"
                            }
                        }
                    },
               {
                   "name": "John2",
                   "provider": "Some Name2",
                   "published": "2016-04-11 00:00:00 -0400",
                   "formats": {
                           "key1": {
                              "width": 2048,
                              "height": 651,
                              "url": "https://some.dummy.url"
                            },
                           "key2": {
                              "width": 2048,
                              "height": 651,
                              "url": "https://some.dummy.url"
                            },
                           "key3": {
                              "width": 2048,
                              "height": 651,
                              "url": "https://some.dummy.url"
                            },
                           "key4": {
                              "width": 2048,
                              "height": 651,
                              "url": "https://some.dummy.url"
                            }
                        }
                    }
                ]
 "20160412" : [
                {
                   "name": "John",
                   "provider": "Some Name",
                   "published": "2016-04-11 00:00:00 -0400",
                   "formats": {
                           "key1": {
                              "width": 2048,
                              "height": 651,
                              "url": "https://some.dummy.url"
                            },
                           "key2": {
                              "width": 2048,
                              "height": 651,
                              "url": "https://some.dummy.url"
                            },
                           "key3": {
                              "width": 2048,
                              "height": 651,
                              "url": "https://some.dummy.url"
                            },
                           "key4": {
                              "width": 2048,
                              "height": 651,
                              "url": "https://some.dummy.url"
                            }
                        }
                    },
               {
                   "name": "John",
                   "provider": "Some Name",
                   "published": "2016-04-11 00:00:00 -0400",
                   "formats": {
                           "key1": {
                              "width": 2048,
                              "height": 651,
                              "url": "https://some.dummy.url"
                            },
                           "key2": {
                              "width": 2048,
                              "height": 651,
                              "url": "https://some.dummy.url"
                            },
                           "key3": {
                              "width": 2048,
                              "height": 651,
                              "url": "https://some.dummy.url"
                            },
                           "key4": {
                              "width": 2048,
                              "height": 651,
                              "url": "https://some.dummy.url"
                            }
                        }
                    }
              ]

   }

类对象

Class MyClass{
    private String name;
    private String provider;
    private Date published;
    private Map<String, Format> formats;
  }

Class Format{
    private int width;
    private int height;
    private String url;
  }

反序列化时我使用以下内容将json字符串转换为object

Type type = new TypeToken<Map<Date, List<MyClass>>>(){}.getType();
Map<Date,List<Comic>> resultMap= gson.fromJson(jsonStr, type);

我面临的问题是我不希望MyClass中格式映射中的key2,key3,key4对应的所有对象(我只想要一个key1)。我希望MyClass看起来如下。

所需课程

Class MyClass{
    private String name;
    private String provider;
    private Date published;
    private Format format;
  }

如何为上述编写自定义反序列化器?

0 个答案:

没有答案