日期:2014-05-16 浏览次数:20441 次
作者: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_use