一种从JSON数据创建Java类的高效办法
《一种从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工具的不足,它实际上能节省你大量的开发时间,也会帮助你减少错误,不错的工具。
一种从JSON数据创建Java类的高效办法的更多相关文章
- Java JSON数据创建和读取
Java json数据创建 package com.JavaTest; import com.google.gson.JsonArray; import com.google.gson.JsonOb ...
- JSON数据与Java对象的相互转换
JSON数据与Java对象的相互转换 JSON解析器 常见的解析器:Jsonlib .Gson. fastjson. jackson JSON转化为Java对象 使用步骤: 1.导入jackson的相 ...
- 【自制工具类】struts返回json数据包装格式类
自己写的一个给struts返回的json数据包装格式类,不喜勿喷,原创,需在项目中引入com.alibaba.fastjson的jar包 先看下效果(这里没有使用msg,有兴趣的往下看): 上demo ...
- 创建java类并实例化类对象
创建java类并实例化类对象例一1.面向对象的编程关注于类的设计2.设计类实际上就是设计类的成员3.基本的类的成员,属性(成员变量)&方法 面向对象思想的落地法则一:1.设计类,并设计类的成员 ...
- Eclipse 创建 Java 类
打开新建 Java 类向导 你可以使用新建 Java 类向导来创建 Java 类,可以通过以下途径打开 Java 类向导: 点击 "File" 菜单并选择 New > Cla ...
- Eclipse 创建 Java 类---Eclipse教程第10课
打开新建 Java 类向导 你可以使用新建 Java 类向导来创建 Java 类,可以通过以下途径打开 Java 类向导: 点击 "File" 菜单并选择 New > Cla ...
- android开发学习 ------- json数据与实体类之间的相互转换
在网络请求的时候,会返回给我们实体类,我们需要将实体类转化为json字符串,方便处理数据: 有时候也会将json数据转换为实体类. 在Android Studio中,json要互相转换,需要用到gso ...
- vs里根据json快速创建对应类的方法
有时候,我们在调用别人接口的时候,服务端返回了一个json格式的字符串,我们要获取json里面的数据的话一般有两种方式: 1.通过正则 2.反序列化成一个对象 第一种方式这里不再多说,主要说一下第二种 ...
- JSON数据和Java对象的相互转换
JSON解析器: 常见的解析器: Jsonlib, Gson, fastjson, jackson 其中应用最广泛的是jackson,阿里的fastjson虽然比jackson快一点,但存在的问题比较 ...
随机推荐
- [itint5]三数和为0
http://www.itint5.com/oj/#20 其实是3sum的变种,有重复数字,但是一开始还是写错了.其实是选定一个后,在右边剩余数组里找2sum,找到一组后继续找. #include & ...
- $.cookie 使用不了的问题定位过程
最近在项目中需要使用到jquery的cookie,按理说在html头中引入jquery-1.7.1.min.js和jquery.cookie.js,然后在js中就可以使用cookie函数了.像这样使用 ...
- Hoax or what
Hoax or what 题意是询问一个动态序列的最小值和最大值. 可以用multiset来实现. #include <stdio.h> #include <set> usin ...
- 【转】五种开源协议的比较(BSD, Apache, GPL, LGPL, MIT)
当 Adobe.Microsoft.Sun 等一系列巨头开始表现出对”开源”的青睐时,”开源”的时代即将到来! 现今存在的开源协议很多,而经过 Open Source Initiative 组织通过批 ...
- visio2010去除直线交叉处的跨线
设计(最上方)->连接线(最右侧)->显示跨线(取消打钩)
- 有关PHP 10条有用的建议
1.使用ip2long() 和long2ip()函数来把IP地址转化成整型存储到数据库里. 这种方法把存储空间降到了接近四分之一(char(15)的15个字节对整形的4个字节),计算一个特定的地址是不 ...
- 【HDOJ】4333 Revolving Digits
扩展KMP基础题目. /* 4333 */ #include <iostream> #include <sstream> #include <string> #in ...
- 大四实习准备3_java多线程
4.25.27无耻地懒散了.....26号陪女朋友去了.今天28号,继续加油! 2015-4-28 Java 多线程 (java中类不能多继承,可以多层继承:接口则都可以) 定义和创建: 方法一:继承 ...
- 在SQL Server实现最短路径的搜索
开始 这是去年的问题了,今天在整理邮件的时候才发现这个问题,感觉顶有意思的,特记录下来. 在表RelationGraph中,有三个字段(ID,Node,RelatedNode),其中Node和Rela ...
- 如何在Azure Websites中配置PHP从而改变系统默认时区
Shirley_Wang Tue, Mar 3 2015 7:29 AM Azure Website为我们提供了可高度扩展的网站部署平台.由于Website是PaaS(平台即服务)层的服务,当用户把 ...