浅谈fastjson的常用使用方法
如下所示:
packageDemo; importjava.util.ArrayList; importjava.util.Collection; importjava.util.Date; importjava.util.HashMap; importjava.util.List; importjava.util.Vector; importcom.alibaba.fastjson.JSON; importcom.alibaba.fastjson.JSONArray; importcom.alibaba.fastjson.JSONObject; importcom.alibaba.fastjson.serializer.SerializerFeature; importentity.Userinfo; /** *fastjson是一个性能很好的Java语言实现的JSON解析器和生成器,来自阿里巴巴的工程师开发。主要特点: *1.快速FAST(比其它任何基于Java的解析器和生成器更快,包括jackson)强大(支持普通JDK类包括任意JavaBean *2.Class、Collection、Map、Date或enum)零依赖(没有依赖其它任何类库除了JDK) * */ publicclassTestFastJson{ publicstaticvoidmain(String[]args){ Stringjson="{\"name\":\"chenggang\",\"age\":24}"; StringarrayAyy="[[\'马云',50],null,[\'马化腾',30]]"; //Entity2json("zhangsan",24); //list2Json(); Complexdata(); //Deserialization(json); //DateFormate(newDate()); //Json2Eetity(json); //String2JSONArray(arrayAyy); } //实体转为Json publicstaticvoidEntity2json(Stringname,intage){ Userinfoinfo=newUserinfo(name,age); Stringstr_json=JSON.toJSONString(info);// System.out.println("实体转化为Json"+str_json); } //list转Json publicstaticvoidlist2Json(){ List<Userinfo>list=newArrayList<Userinfo>(); Userinfouserinfo1=newUserinfo("lisi",15); Userinfouserinfo2=newUserinfo("wangwu",16); list.add(userinfo1); list.add(userinfo2); Stringjson=JSON.toJSONString(list,true); System.out.println("List集合转json格式字符串:"+json); } //字符数组转化为JSon privatestaticvoidString2JSONArray(StringarrayAyy){ JSONArrayarray=JSONArray.parseArray(arrayAyy); System.out.println("数组:"+array); System.out.println("数组长度:"+array.size()); CollectionnuCon=newVector(); nuCon.add(null); array.removeAll(nuCon); System.out.println("数组:"+array); System.out.println("数组长度:"+array.size()); } //复杂数据类型 publicstaticvoidComplexdata(){ HashMap<String,Object>map=newHashMap<String,Object>(); map.put("username","zhangsan"); map.put("age",24); map.put("sex","男"); //map集合 HashMap<String,Object>temp=newHashMap<String,Object>(); temp.put("name","xiaohong"); temp.put("age","23"); map.put("girlInfo",temp); //list集合 List<String>list=newArrayList<String>(); list.add("爬山"); list.add("骑车"); list.add("旅游"); map.put("hobby",list); StringjsonString=JSON.toJSONString(map); System.out.println("复杂数据类型:"+jsonString); } publicstaticvoidDeserialization(Stringjson){ UserinfouserInfo=JSON.parseObject(json,Userinfo.class); System.out.println("姓名是:"+userInfo.getName()+",年龄是:" +userInfo.getAge()); } //格式话日期 publicstaticvoidDateFormate(Datedate){ System.out.println("输出毫秒值:"+JSON.toJSONString(date)); System.out.println("默认格式为:" +JSON.toJSONString(date, SerializerFeature.WriteDateUseDateFormat)); System.out.println("自定义日期:" +JSON.toJSONStringWithDateFormat(date,"yyyy-MM-dd", SerializerFeature.WriteDateUseDateFormat)); } //Json转为实体 privatestaticvoidJson2Eetity(Stringjson){ UserinfouserInfo=JSON.parseObject(json,Userinfo.class); System.out.println("输出对象的地址:"+userInfo.toString()); System.out.println("输出对象的名字:"+userInfo.getName()); } }
以上Demo所用到的实体类:
packageentity; publicclassUserinfo{ privatestaticfinallongserialVersionUID=1L; privateStringname; privateintage; publicUserinfo(){ super(); } publicUserinfo(Stringname,intage){ super(); this.name=name; this.age=age; } publicvoidsetName(Stringname){ this.name=name; } publicStringgetName(){ returnname; } publicvoidsetAge(intage){ this.age=age; } publicintgetAge(){ returnage; } }
以上这篇浅谈fastjson的常用使用方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。