一种从JSON数据创建Java类的高效办法
作者:chszs,转载需注明。博客主页:http://blog.csdn.net/chszs
JSON格式的数据经常会遇到,比如调用Web服务,取回的数据通常就是JSON格式的。如何高效地把JSON数据转换成实际的Java类对象,就是本文要说明的问题。
写一个操纵JSON数据的Java程序,通常代码会重度依赖于JSON API,你总是需要对JSON数据进行反序列化,再转换成原生Java对象。整个过程大致如下:
1)下载所有的JSON响应;
2)分析JSON对象的结构,映射到Java类;
3)手动煞费苦心地创建每一个Java类,键入每个Java类的私有属性名和数据类型,以匹配JSON所有对象的属性;
4)为每个Java类创建public类型的getter和setter方法。
- package com.cypressnorth.demo.models.twitter;
- import java.util.List;
- public class TwitterItem{
- private String contributors;
- private transient Geo coordinates;
- private String created_at;
- private Entities entities;
- private Number favorite_count;
- private boolean favorited;
- private Geo geo;
- private Number id;
- private String id_str;
- private String in_reply_to_screen_name;
- private String in_reply_to_status_id;
- private String in_reply_to_status_id_str;
- private String in_reply_to_user_id;
- private String in_reply_to_user_id_str;
- private String lang;
- private boolean possibly_sensitive;
- private Number retweet_count;
- private boolean retweeted;
- private Retweeted_status retweeted_status;
- private String source;
- private String text;
- private boolean truncated;
- private User user;
- public TwitterItem(){}
- public String getContributors(){
- return this.contributors;
- }
- public void setContributors(String contributors){
- this.contributors = contributors;
- }
- public Geo getCoordinates(){
- return this.coordinates;
- }
- public void setCoordinates(Geo coordinates){
- this.coordinates = coordinates;
- }
- public String getCreated_at(){
- return this.created_at;
- }
- public void setCreated_at(String created_at){
- this.created_at = created_at;
- }
- public Entities getEntities(){
- return this.entities;
- }
- public void setEntities(Entities entities){
- this.entities = entities;
- }
- public Number getFavorite_count(){
- return this.favorite_count;
- }
- public void setFavorite_count(Number favorite_count){
- this.favorite_count = favorite_count;
- }
- public boolean getFavorited(){
- return this.favorited;
- }
- public void setFavorited(boolean favorited){
- this.favorited = favorited;
- }
- public Geo getGeo(){
- return this.geo;
- }
- public void setGeo(Geo geo){
- this.geo = geo;
- }
- public Number getId(){
- return this.id;
- }
- public void setId(Number id){
- this.id = id;
- }
- public String getId_str(){
- return this.id_str;
- }
- public void setId_str(String id_str){
- this.id_str = id_str;
- }
- public String getIn_reply_to_screen_name(){
- return this.in_reply_to_screen_name;
- }
- public void setIn_reply_to_screen_name(String in_reply_to_screen_name){
- this.in_reply_to_screen_name = in_reply_to_screen_name;
- }
- public String getIn_reply_to_status_id(){
- return this.in_reply_to_status_id;
- }
- public void setIn_reply_to_status_id(String in_reply_to_status_id){
- this.in_reply_to_status_id = in_reply_to_status_id;
- }
- public String getIn_reply_to_status_id_str(){
- return this.in_reply_to_status_id_str;
- }
- public void setIn_reply_to_status_id_str(String in_reply_to_status_id_str){
- this.in_reply_to_status_id_str = in_reply_to_status_id_str;
- }
- public String getIn_reply_to_user_id(){
- return this.in_reply_to_user_id;
- }
- public void setIn_reply_to_user_id(String in_reply_to_user_id){
- this.in_reply_to_user_id = in_reply_to_user_id;
- }
- public String getIn_reply_to_user_id_str(){
- return this.in_reply_to_user_id_str;
- }
- public void setIn_reply_to_user_id_str(String in_reply_to_user_id_str){
- this.in_reply_to_user_id_str = in_reply_to_user_id_str;
- }
- public String getLang(){
- return this.lang;
- }
- public void setLang(String lang){
- this.lang = lang;
- }
- public boolean getPossibly_sensitive(){
- return this.possibly_sensitive;
- }
- public void setPossibly_sensitive(boolean possibly_sensitive){
- this.possibly_sensitive = possibly_sensitive;
- }
- public Number getRetweet_count(){
- return this.retweet_count;
- }
- public void setRetweet_count(Number retweet_count){
- this.retweet_count = retweet_count;
- }
- public boolean getRetweeted(){
- return this.retweeted;
- }
- public void setRetweeted(boolean retweeted){
- this.retweeted = retweeted;
- }
- public Retweeted_status getRetweeted_status(){
- return this.retweeted_status;
- }
- public void setRetweeted_status(Retweeted_status retweeted_status){
- this.retweeted_status = retweeted_status;
- }
- public String getSource(){
- return this.source;
- }
- public void setSource(String source){
- this.source = source;
- }
- public String getText(){
- return this.text;
- }
- public void setText(String text){
- this.text = text;
- }
- public boolean getTruncated(){
- return this.truncated;
- }
- public void setTruncated(boolean truncated){
- this.truncated = truncated;
- }
- public User getUser(){
- return this.user;
- }
- public void setUser(User user){
- this.user = user;
- }
- }
整个过程显然很耗时间,而且还容易出现键入错误或数据类型匹配错误。
一、自动生成Java存根Stub
在线网站:http://jsongen.byingtondesign.com/
它提供了JSON解析并对JSON数据结构进行建模,生成Java类的功能。你可以自定义包名,输出的内容是一个ZIP文件,里面根据包名路径,包含生成的Java实体类。
你可以把得到的Java类文件放入到你的项目中,以便对JSON访问反序列化/序列化时使用。
二、注意事项
此工具能节省不少时间,然而,它不是一劳永逸的解决方案。
JSON数据的一个显著缺点是其集合或属性的数据类型并不能通过程序100%精准的判断,这是因为数据的展现是宽松的。比如,一个整数值可以被表示为“1”或者1。而JSON Gen工具并不能确定“1”是整数还是字符串,因此你最终会得到大量的字符串类型的属性。所以,需要你手动地去检查每一个生成的Java类,看所有的私有属性的数据类型是否正确。
此工具另一个潜在的问题是它在运行时只能关注对象,如果API响应变化,生成的Java文件或许会丢失部分元素。
三、节省时间
除开JSON Gen工具的不足,它实际上能节省你大量的开发时间,也会帮助你减少错误,不错的工具。