GSON中的自定义反序列化? [英] Custom deserialization in GSON?

查看:93
本文介绍了GSON中的自定义反序列化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建一个自定义的反序列化器,这样我才能正确反序列化我以这种格式接收的日期: 2011-10-19T23:30:00-04:00 。我的Date对象是许多字段之一,包含我反序列化的Object的许多嵌套类之一。除了日期之外,一切正常。我的课程如下所示:

I need to create a custom deserializer so I can correctly deserialize a date that I'm receiving in this format: 2011-10-19T23:30:00-04:00. My Date object is one of many fields, contained one of many nested classes of the Object that I'm deserializing. Everything works fine except for the Date. My class looks like this:

public class OfferContainer{

    public Offer offer;

    public OfferContainer(){}   

    @Override
    public String toString()
    {
        return this.getID() +  offer.toString();
    }

    public String getDescription() {
        return offer.description;
    }

    public String getBusinessName() {
        return offer.business.name;
    }



    public class Offer
    {
        public Category category;
        public String description;
        public String discount;
        public Date expiration;
        public Date published;
        public String rescinded_at;
        public String title;
        public String hook;
        public Date valid_from;
        public Date valid_to;
        public String id;
        public Business business;
        public Location location;
        public String image_270x155;


        @Override
        public String toString()
        {
            return String.format(
                    "[Offer: category=%1$s, description=%2$s, discount=%3$s, expiration=%4$s, published=%5$s, rescinded_at=%6$s, title=%7$s, valid_from=%8$s, valid_to=%9$s, id=%10$s, business=%11$s, location=%12$s]",
                    category, description, discount, expiration, published, rescinded_at, title, valid_from, valid_to, id,
                    business, location);
        }

    }

   public enum Category
    {
        Salon, Spa, Restaurant, Other
    }



    public class Business
    {
        public String name;
        public String phone;
        public Address address;


        @Override
        public String toString()
        {
            return String.format(
                    "[Business: name=%1$s, phone=%2$s, address=%3$s]",
                    name, phone, address);
        }
    }


    public  class Address
    {
        public String address_1;
        public String address_2;
        public String city;
        public String cross_streets;
        public String state;
        public String zip;



        @Override
        public String toString()
        {
            return String.format(
                    "[Address: address_1=%1$s, address_2=%2$s, city=%3$s, cross_streets=%4$s, state=%5$s, zip=%6$s]",
                    address_1, address_2, city, cross_streets, state, zip);
        }
    }

    public class Location {
        public double latitude;
        public double longitude;



        public String toString() {
            return String.format("[Location: longitude=%1$s, latitude=%2$s]", longitude, latitude);
        }

    }

}

如何在这种情况下使用 registerTypeAdapter ,我该如何正确实现一个自定义的反序列化器?

How do I use registerTypeAdapter in this scenario and how do I correctly implement a custom deserializer for this?

另外,如果有问题,以下是我如何获得我的POJO:

Also, if it matters, here's how I'm actually getting my POJO:

  OfferContainer[] offers = gson.fromJson(json, OfferContainer[].class);

以下是JSON:

Here's the JSON:

{
    "offer":{
        "category":"Salon",
        "description":"Use this offer now to enjoy this great Salon at a 20% discount. ",
        "discount":"20",
        "expiration":"2011-04-08T02:30:00Z",
        "published":"2011-04-07T12:00:33Z",
        "rescinded_at":null,
        "title":"20% off at Jun Hair Salon",
        "valid_from":"2011-04-07T12:00:31Z",
        "valid_to":"2011-04-08T02:00:00Z",
        "id":"JUN_HAIR_1302177631",
        "business":{
            "name":"Jun Hair Salon",
            "phone":"2126192989",
            "address":{
                "address_1":"12 Mott St",
                "address_2":null,
                "city":"New York",
                "cross_streets":"Chatham Sq & Worth St",
                "state":"NY",
                "zip":"10013"
            }
        },
        "location":{
            "latitude":"40.714021",
            "longitude":"-73.998663"
        },
        "distance":619.8162766744034,
        "draws":[
            "Hair Cut",
            "Blow Dry",
            "Blow Dry Treatment"
        ],
        "claim_link":"http://m-s.com/offers/JUN_HAIR_1302177631?app_id=SavIMg",
        "mobile_claim_link":"http://m-s.com/offers/JUN_HAIR_1302177631?app_id=SavIMg"
    }
}


推荐答案

以实现 JSONDeserializer< OfferContainer> ,然后重写 deserialize 方法来实现此目的。看看这个SO问题 GSON反序列化自定义对象的键值

You need to implement JSONDeserializer<OfferContainer> and then override the deserialize method to achieve this. Have a look at this SO question GSON deserializing key-value to custom object

您也可以尝试在 GsonBuilder 中设置dateformat像这样

You can also try setting the dateformat in the GsonBuilder like this

 Gson gson = new GsonBuilder()
     .setDateFormat("yyyy-MM-dd'T'HHmmss.SSS'Z'")
     .create();
 OfferContainer [] offers = gson.fromJson(json, OfferContainer[].class);

这篇关于GSON中的自定义反序列化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆