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

javascript[ { "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

已经有 ExploresBoardsUsers 三个类

设计了一个 Recommendation

recommendation数据库是 recommendation_idstatustypeseqtitleurl 字段

jackson 里面有一个 @JsonUnwrapped 注解

@JsonUnwrapped 作用在属性字段或方法上,用来将子JSON对象的属性添加到封闭的JSON对象

如果没有@JsonUnwrapped,序列化后将为{"id":111,"name":{"firstName":"张","secondName":"三"}}

反之:{"id":111,"firstName":"张","secondName":"三"}

不这个 GSON 有没有这个方法

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.fromJsonjson,type;
// TODO

public class Item { private Long recommendationId; private Integer status; private String type; ... private User user; private Cover cover;
} public class User { private Long userId; private String username; ...
} public class Cover { private String key; private String bucket;
}

上面的代码省去了 getter 和 setter。我看基本上照这样做就是了,没有什么特别的地方。

发表评论

电子邮件地址不会被公开。 必填项已用*标注