一種從JSON資料建立Java類的高效辦法
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow
也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!
《一種從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工具的不足,它實際上能節省你大量的開發時間,也會幫助你減少錯誤,不錯的工具。