《一种从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类的高效办法的更多相关文章

  1. Java JSON数据创建和读取

    Java  json数据创建 package com.JavaTest; import com.google.gson.JsonArray; import com.google.gson.JsonOb ...

  2. JSON数据与Java对象的相互转换

    JSON数据与Java对象的相互转换 JSON解析器 常见的解析器:Jsonlib .Gson. fastjson. jackson JSON转化为Java对象 使用步骤: 1.导入jackson的相 ...

  3. 【自制工具类】struts返回json数据包装格式类

    自己写的一个给struts返回的json数据包装格式类,不喜勿喷,原创,需在项目中引入com.alibaba.fastjson的jar包 先看下效果(这里没有使用msg,有兴趣的往下看): 上demo ...

  4. 创建java类并实例化类对象

    创建java类并实例化类对象例一1.面向对象的编程关注于类的设计2.设计类实际上就是设计类的成员3.基本的类的成员,属性(成员变量)&方法 面向对象思想的落地法则一:1.设计类,并设计类的成员 ...

  5. Eclipse 创建 Java 类

    打开新建 Java 类向导 你可以使用新建 Java 类向导来创建 Java 类,可以通过以下途径打开 Java 类向导: 点击 "File" 菜单并选择 New > Cla ...

  6. Eclipse 创建 Java 类---Eclipse教程第10课

    打开新建 Java 类向导 你可以使用新建 Java 类向导来创建 Java 类,可以通过以下途径打开 Java 类向导: 点击 "File" 菜单并选择 New > Cla ...

  7. android开发学习 ------- json数据与实体类之间的相互转换

    在网络请求的时候,会返回给我们实体类,我们需要将实体类转化为json字符串,方便处理数据: 有时候也会将json数据转换为实体类. 在Android Studio中,json要互相转换,需要用到gso ...

  8. vs里根据json快速创建对应类的方法

    有时候,我们在调用别人接口的时候,服务端返回了一个json格式的字符串,我们要获取json里面的数据的话一般有两种方式: 1.通过正则 2.反序列化成一个对象 第一种方式这里不再多说,主要说一下第二种 ...

  9. JSON数据和Java对象的相互转换

    JSON解析器: 常见的解析器: Jsonlib, Gson, fastjson, jackson 其中应用最广泛的是jackson,阿里的fastjson虽然比jackson快一点,但存在的问题比较 ...

随机推荐

  1. Servlet课程0426(八)Servlet分页技术

    Welcome.java //登录界面 package com.tsinghua; import javax.servlet.http.*; import java.io.*; import java ...

  2. Qt xcode wrapper Qios OpenFly

    https://github.com/richardmg/QtWrapper https://github.com/richardmg/qios https://github.com/richardm ...

  3. git clone 出错SSL certificate problem, verify that the CA cert is OK.

    先调用这个 export GIT_SSL_NO_VERIFY=true 之后再执行git clone

  4. Java版本的删除指定目录及子目录下名叫“xxx.txt”的所有文件

    以前写过一个python版本的,但是在查找文件路径的时候出现错误,无法正确的获取到文件的路径,就造成无法删除该路径下的“xxx.txt”文件. 当时以为是windows版本系统的错误造成这个问题的,也 ...

  5. compass和sass很好的两篇文章

    Sass是一种"CSS预处理器",可以让CSS的开发变得简单和可维护.但是,只有搭配Compass,它才能显出真正的威力. 本文介绍Compass的用法.毫不夸张地说,学会了Com ...

  6. Spring Data JPA初使用

    我们都知道Spring是一个非常优秀的JavaEE整合框架,它尽可能的减少我们开发的工作量和难度. 在持久层的业务逻辑方面,Spring开源组织又给我们带来了同样优秀的Spring Data JPA. ...

  7. 关于 tomcat 集群中 session 共享的三种方法

    前两种均需要使用 memcached 或redis 存储 session ,最后一种使用 terracotta 服务器共享. 建议使用 redis,不仅仅因为它可以将缓存的内容持久化,还因为它支持的单 ...

  8. LeetCode Minimum Size Subarray Sum (最短子序列和)

    题意:给一个序列,找出其中一个连续子序列,其和大于s但是所含元素最少.返回其长度.0代表整个序列之和均小于s. 思路:O(n)的方法容易想.就是扫一遍,当子序列和大于s时就一直删减子序列前面的一个元素 ...

  9. HDU4612 Warm up 边双(重边)缩点+树的直径

    题意:一个连通无向图,问你增加一条边后,让原图桥边最少 分析:先边双缩点,因为连通,所以消环变树,每一个树边都是桥,现在让你增加一条边,让桥变少(即形成环) 所以我们选择一条树上最长的路径,连接两端, ...

  10. MapReduce:Shuffle过程的流程

    Shuffle过程是MapReduce的核心,Shuffle描述着数据从map task输出到reduce task输入的这段过程. 1.map端