开发者社区> 问答> 正文

根据list集合设计类问题

蛮大人123 2016-02-28 17:21:17 1163

像这样的 List 集合 java 类怎么设计

[
    {
        "recommendation_id": 586,
        "status": 1,
        "type": "explores",
        "seq": 597,
        "keyword_id": 848,
        "name": "门头设计",
        "urlname": "mentousheji",
        "cover": {
            "bucket": "hbfile",
            "key": "dd3eb7af6bf69adfd8ee8be24703e9fe10041df61f164"
        },
        "description": "门头,是指企业、事业单位和个体工商户(以下简称单位)在门口设置的牌匾及相关设施。门头是一个商铺店门外的装饰形式。整体上说,制作精美的门头是美化销售场所和装饰店铺、吸引顾客的一种手段,是提升店面形象的重要方法。精心设计门头,是现代装饰的重要内容。",
        "recommended_users": null,
        "creator_id": null,
        "is_hidden": 0,
        "follow_count": 13,
        "target_id": 848,
        "title": "门头设计",
        "url": "http://huaban.com/explore/mentousheji/"
    },
    {
        "recommendation_id": 584,
        "status": 1,
        "type": "boards",
        "seq": 194,
        "board_id": 18682745,
        "user_id": 327260,
        "title": "◤各种黑白海报 | Black&White◢",
        "description": "",
        "category_id": "design",
        "pin_count": 161,
        "follow_count": 214,
        "like_count": 1,
        "created_at": 1417504894,
        "updated_at": 1420450108,
        "deleting": 0,
        "is_private": 0,
        "user": {
            "username": "花蓉",
            "urlname": "nininini757",
            "pin_count": 20165,
            "follow_count": 28573,
            "user_id": 327260
        },
        "target_id": 18682745,
        "cover": {
            "bucket": "hbfile",
            "key": "a81b9392c3a197d596ec9ae4d337535295554e9d1a2df"
        },
        "url": "http://huaban.com/boards/18682745/"
    },
    {
        "recommendation_id": 583,
        "status": 1,
        "type": "users",
        "seq": 594,
        "user_id": 9893639,
        "username": "我爱的人终变成我心里的针",
        "urlname": "love734889400",
        "created_at": 1379850964,
        "avatar": {
            "id": 29775572,
            "farm": "farm1",
            "bucket": "hbimg",
            "key": "69706c1194d0cc588a948fdbfb4628227dc1292b163d-bW6ftL",
            "type": "image/jpeg",
            "width": 139,
            "height": 144,
            "frames": 1
        },
        "pin_count": 5222,
        "follow_count": 30138,
        "target_id": 9893639,
        "cover": {
            "bucket": "",
            "key": ""
        },
        "title": "我爱的人终变成我心里的针",
        "url": "http://huaban.com/love734889400/"
    }
]

用的是 GSON 转json已经有 Explores,Boards,Users 三个类设计了一个 Recommendation 类
recommendation数据库是 recommendation_id,status,type,seq,title,url 字段jackson 里面有一个 @JsonUnwrapped 注解
@JsonUnwrapped 作用在属性字段或方法上,用来将子JSON对象的属性添加到封闭的JSON对象如果没有@JsonUnwrapped,序列化后将为{"id":111,"name":{"firstName":"张","secondName":"三"}}反之:{"id":111,"firstName":"张","secondName":"三"}不这个 GSON 有没有这个方法

JSON Java 数据库 数据格式
分享到
取消 提交回答
全部回答(1)
  • 蛮大人123
    2019-07-17 18:49:50

    json的键就是类的字段名,看见一个键后面是“{”就创建一个类,看见键后面是“[”就是一个List,这样就简单了。Recommendation里包含Cover、Avatar、User三个类,分别对应cover、avatar和user字段
    //类定义

    public class Recommendation{
        public long recommendationId;
        //...
        public User user;
        public Cover cover;
        public Avatar avatar;
    }
    public class User {
        private Long userId;
        private String username;
        ...
    }
    public class Cover {
        private String key;
        private String bucket;
    }
    public class Avatar{
        private int id;
        // ...
        private int width;
        private int height;
        private int frames;
    }
    //解析
    Gson gson = new Gson();
    Type type = new TypeToken<List<Recommendation>>(){}.getType();//最外面是个List
    String json = "json内容";//String也可以是一个Reader
    List<Recommendation> = gson.fromJson(json,type);
    // TODO
    0 0

集结各类场景实战经验,助你开发运维畅行无忧

推荐文章
相似问题