版權聲明:本文為部落客chszs的原創文章,未經部落客允許不得轉載。 https://blog.csdn.net/chszs/article/details/12585791
《一種從JSON資料建立Java類的高效辦法》
作者:chszs,轉載需注明。部落格首頁:
http://blog.csdn.net/chszsJSON格式的資料經常會遇到,比如調用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工具的不足,它實際上能節省你大量的開發時間,也會幫助你減少錯誤,不錯的工具。