您好,登錄后才能下訂單哦!
研究了下FastJson的使用方法,感覺比較好使,可以創建json字符串,也可以根據json字符串來創建對象
* 1、class Json.parseObject(String,clazz.class);//將一個JSON字符串轉換成約定的Java實體對象
* 2、String Json.toString(class,boolean);//將一個實體對象轉化成JSON字符串
* 3、String Json.toJSONString(collection,boolean);//將一個集合中的數據轉化成JSON字符串
* 4、JSONArray Json.parseJSONArray(String);//將一個JSON字符串轉換成JSONArray
package com.flag.app; import java.util.ArrayList; import java.util.Collection; import java.util.List; import java.util.Vector; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.flag.app.Entity.Welcome; /** * 主要使用的API有下面幾個: * * 1、class Json.parseObject(String,clazz.class);//將一個JSON字符串轉換成約定的Java實體對象 * 2、String Json.toString(class,boolean);//將一個實體對象轉化成JSON字符串 * 3、String Json.toJSONString(collection,boolean);//將一個集合中的數據轉化成JSON字符串 * 4、JSONArray Json.parseJSONArray(String);//將一個JSON字符串轉換成JSONArray * * @author Administrator * */ public class FastJsonMain { static String json = "{\"study\": 10,\"study_course_count\": 10, \"recieved_reg_form_count\": 0,\"unread_count\": 5,\"teach_course_count\": 8,\"avatar\": \"1316634098115-02-57\",\"user_id\": 201,\"nickname\": \"Asus人名\"}"; static String arrayAyy="[[14,\"小學語文\"],[154,\"美食\"],[72,\"高中物理\"],null,[50,\"高中化學\"],[15,\"小學數學\"],[13\"英語\"],null,[1,\"其他英語培訓\"],null]"; /** * Json 轉換 實體類 提示:FastJson在映射實體上非常棒, json有的K,實體沒有,依然不影響解析.. */ private void Json2Eetity() { // TODO Auto-generated method stub Welcome welcome = JSON.parseObject(json, Welcome.class); System.out.println(welcome.toString()); System.out.println(welcome.getNickname()); } /** * 實體類 轉換 Json */ private void Eetity2Json() { // TODO Auto-generated method stub Welcome welcome = new Welcome(2, 3, 4, 5, "imagUrl", 11, "Mers"); String json = JSON.toJSONString(welcome, true); System.out.println("實體轉換Json:" + json); } /** * list集合轉json格式字符串 */ public void list2Json() { List<Welcome> list = new ArrayList<Welcome>(); Welcome welcome1 = new Welcome(2, 3, 4, 5, "imagUrl", 11, "Mers"); Welcome welcome2 = new Welcome(22, 33, 44, 55, "imag", 65, "Kers"); Welcome welcome3 = new Welcome(64, 33, 34, 05, "imagUrl", 43, "Wers"); Welcome welcome4 = new Welcome(62, 75, 41, 25, "imagUrl", 109, "Oers"); list.add(welcome1); list.add(welcome2); list.add(welcome3); list.add(welcome4); String json = JSON.toJSONString(list, true); System.out.println("ist集合轉json格式字符串 :" + json); } /** * String轉換 JSONArray * 并且去除null */ private void String2JSONArray() { // TODO Auto-generated method stub JSONArray array=JSONArray.parseArray(arrayAyy); System.out.println(array); System.out.println("長度: "+array.size()); Collection nuCon = new Vector(); nuCon.add(null); array.removeAll(nuCon); System.out.println(array); System.out.println("長度: "+array.size()); } public static void main(String[] args) { FastJsonMain main = new FastJsonMain(); main.Json2Eetity(); System.out.println(" "); main.Eetity2Json(); System.out.println(""); main.list2Json(); System.out.println(" "); main.String2JSONArray(); } } package com.flag.app.Entity; public class Welcome { /** * 報名的個數 */ public int study_course_count; /** * 收到報名個數 */ public int recieved_reg_form_count; /** * push推送課程報名個數 */ public int unread_count; /** * 開設課程 */ public int teach_course_count; /** * 用戶頭像 */ public String avatar; /** * 用戶ID */ public int user_id; /** * 用戶名 */ public String nickname; public Welcome() { super(); // TODO Auto-generated constructor stub } public Welcome(int study_course_count, int recieved_reg_form_count, int unread_count, int teach_course_count, String avatar, int user_id, String nickname) { super(); this.study_course_count = study_course_count; this.recieved_reg_form_count = recieved_reg_form_count; this.unread_count = unread_count; this.teach_course_count = teach_course_count; this.avatar = avatar; this.user_id = user_id; this.nickname = nickname; } public int getStudy_course_count() { return study_course_count; } public void setStudy_course_count(int study_course_count) { this.study_course_count = study_course_count; } public int getRecieved_reg_form_count() { return recieved_reg_form_count; } public void setRecieved_reg_form_count(int recieved_reg_form_count) { this.recieved_reg_form_count = recieved_reg_form_count; } public int getUnread_count() { return unread_count; } public void setUnread_count(int unread_count) { this.unread_count = unread_count; } public int getTeach_course_count() { return teach_course_count; } public void setTeach_course_count(int teach_course_count) { this.teach_course_count = teach_course_count; } public String getAvatar() { return avatar; } public void setAvatar(String avatar) { this.avatar = avatar; } public int getUser_id() { return user_id; } public void setUser_id(int user_id) { this.user_id = user_id; } public String getNickname() { return nickname; } public void setNickname(String nickname) { this.nickname = nickname; } @Override public String toString() { return "Welcome [study_course_count=" + study_course_count + ", recieved_reg_form_count=" + recieved_reg_form_count + ", unread_count=" + unread_count + ", teach_course_count=" + teach_course_count + ", avatar=" + avatar + ", user_id=" + user_id + ", nickname=" + nickname + "]"; } } 執行的結果如下: Welcome [study_course_count=10, recieved_reg_form_count=0, unread_count=5, teach_course_count=8, avatar=1316634098115-02-57, user_id=201, nickname=Asus人名] Asus人名 實體轉換Json:{ "avatar":"imagUrl", "nickname":"Mers", "recieved_reg_form_count":3, "study_course_count":2, "teach_course_count":5, "unread_count":4, "user_id":11 } ist集合轉json格式字符串 :[ { "avatar":"imagUrl", "nickname":"Mers", "recieved_reg_form_count":3, "study_course_count":2, "teach_course_count":5, "unread_count":4, "user_id":11 }, { "avatar":"imag", "nickname":"Kers", "recieved_reg_form_count":33, "study_course_count":22, "teach_course_count":55, "unread_count":44, "user_id":65 }, { "avatar":"imagUrl", "nickname":"Wers", "recieved_reg_form_count":33, "study_course_count":64, "teach_course_count":5, "unread_count":34, "user_id":43 }, { "avatar":"imagUrl", "nickname":"Oers", "recieved_reg_form_count":75, "study_course_count":62, "teach_course_count":25, "unread_count":41, "user_id":109 } ] [[14,"小學語文"],[154,"美食"],[72,"高中物理"],null,[50,"高中化學"],[15,"小學數學"],[13,"英語"],null,[1,"其他英語培訓"],null] 長度: 10 [[14,"小學語文"],[154,"美食"],[72,"高中物理"],[50,"高中化學"],[15,"小學數學"],[13,"英語"],[1,"其他英語培訓"]] 長度: 7
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。