热门IT资讯网

FastJson的使用

发表于:2024-11-24 作者:热门IT资讯网编辑
编辑最后更新 2024年11月24日,研究了下FastJson的使用方法,感觉比较好使,可以创建json字符串,也可以根据json字符串来创建对象* 1、class Json.parseObject(String,clazz.class)

研究了下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 list = new ArrayList();                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



附件:http://down.51cto.com/data/2365565
0