通过FastJson把字符串转换成JSON和Map和List对象处理json数据

前端之家收集整理的这篇文章主要介绍了通过FastJson把字符串转换成JSON和Map和List对象处理json数据前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

原文:http://blog.csdn.net/jilongliang/article/details/42870951

Fastjson是一个Java语言编写的高性能功能完善的JSON库。

Fastjson是一个Java语言编写的JSON处理器,由阿里巴巴公司开发。

1、遵循http://json.org标准,为其官方网站收录的参考实现之一。

2、功能qiang打,支持JDK的各种类型,包括基本的JavaBean、Collection、Map、Date、Enum、泛型。

3、无依赖,不需要例外额外的jar,能够直接跑在JDK上。

4、开源,使用Apache License 2.0协议开源。http://code.alibabatech.com/wiki/display/FastJSON/Home

5、具有超高的性能java世界里没有其他的json库能够和fastjson可相比了。

性能

fastjson采用独创的算法,将parse的速度提升到极致,超过所有json库,包括曾经号称最快的jackson。并且还超越了google的二进制协议protocol buf。

支持标准

Fastjson完全支持http://json.org的标准,也是官方网站收录的参考实现之一。

功能强大

支持各种JDK类型。包括基本类型、JavaBean、Collection、Map、Enum、泛型等。

支持循环引用

无依赖

不需要例外额外的jar,能够直接跑在JDK上。

支持范围广

支持JDK 5、JDK 6、Android阿里云手机等环境。

开源

Apache License 2.0

代码托管在github.org上,项目地址是 https://github.com/AlibabaTech/fastjson

测试充分

fastjson有超过1500个testcase,每次构建都会跑一遍,丰富的测试场景保证了功能稳定。

获得fastjson

下载

http://code.alibabatech.com/mvn/releases/com/alibaba/fastjson/

maven

  1. packageivyy.taobao.com.domain.fastjson;
  2. importivyy.taobao.com.entity.Student;
  3. importjava.util.ArrayList;
  4. importjava.util.Arrays;
  5. importjava.util.HashMap;
  6. importjava.util.List;
  7. importjava.util.Map;
  8. importcom.alibaba.fastjson.JSON;
  9. importcom.alibaba.fastjson.JSONObject;
  10. importcom.alibaba.fastjson.TypeReference;
  11. /**
  12. *@DEMO:json
  13. *@Java:FastJSON.java
  14. *@Date:2015-1-19上午10:28:12
  15. *@Author:龙叔
  16. *@Email:jilongliang@sina.com
  17. *@Weibo:http://weibo.com/jilongliang
  18. *@Version:1.0
  19. *@Description:fastjson跟json-lib是语法很像,一句话说,所有json都差不多,
  20. *大家伙也没不要研究那么多,懂一种自己最擅长而且熟悉能解决自己要解决的问题就OK,
  21. *从fastjson反编译过来看你就看到pom.xml里面的配置肯定能看到json-lib,此时能
  22. *证明fastjson就是运用了json-lib!
  23. *
  24. *--------------------------------------------
  25. *<dependency>
  26. <groupId>org.codehaus.jackson</groupId>
  27. <artifactId>jackson-smile</artifactId>
  28. <version>1.9.9</version>
  29. <scope>test</scope>
  30. </dependency>
  31. *--------------------------------------------
  32. <dependency>
  33. <groupId>com.googlecode.json-simple</groupId>
  34. <artifactId>json-simple</artifactId>
  35. <version>1.1</version>
  36. <scope>test</scope>
  37. </dependency>
  38. --------------------------------------------
  39. <groupId>net.sf.json-lib</groupId>
  40. <artifactId>json-lib</artifactId>
  41. <version>2.4</version>
  42. <classifier>jdk15</classifier>
  43. */
  44. publicclassFastJSON{
  45. /**
  46. *@paramargs
  47. */
  48. staticvoidmain(String[]args)throwsException{
  49. //string2Json();
  50. //string2Object();
  51. //string2List();
  52. map2json();
  53. map2JSON();
  54. }
  55. *通过fastjson把字符串转换成JSON数据
  56. *TypeReference
  57. voidstring2Json(){
  58. StringBufferbuffer=newStringBuffer();
  59. buffer.append("{");
  60. @H_426_502@"\"age\":").append("27").append(",");
  61. "\"userName\":").append("\"龙叔\"").append();
  62. @H_426_502@"\"address\":").append("\"广东省云浮市\"");
  63. "}");
  64. StringjsonText=buffer.toString();
  65. JSONObjectjobj=JSON.parSEObject(jsonText);
  66. Stringaddress=jobj.get("address").toString();
  67. System.out.println(address);
  68. *通过fastjson把字符串转换成对象
  69. voidstring2Object(){
  70. //方法一把json字符串转成Student对象
  71. Studentstu1=JSON.parSEObject(jsonText,newTypeReference<Student>(){});
  72. //方法二把json字符串转成Student对象
  73. Studentstu2=JSON.parSEObject(jsonText,Student.class);
  74. System.out.println(stu1.getAddress());
  75. System.out.println(stu2.getAddress());
  76. }
  77. *通过fastjson把字符串转换成泛型数组
  78. voidstring2List(){
  79. @H_426_502@"[{"); @H_426_502@"}]");
  80. //转成成数组
  81. Student[]stu2=JSON.parSEObject(jsonText,255); background-color:inherit">newTypeReference<Student[]>(){});
  82. List<Student>list=Arrays.asList(stu2);
  83. for(Studentst:list){
  84. System.out.println(st.getAddress());
  85. //转换成ArrayList
  86. ArrayList<Student>list2=JSON.parSEObject(jsonText,255); background-color:inherit">newTypeReference<ArrayList<Student>>(){});
  87. for(inti=0;i<list2.size();i++){
  88. Studentobj=(Student)list2.get(i);
  89. System.out.println(obj.getAddress());
  90. *通过fastjson把Map换成字符串转
  91. voidmap2json(){
  92. //创建一个Map对象
  93. Map<String,String>map=newHashMap<String,String>();
  94. map.put("username","周伯通");
  95. @H_426_502@"address",0); background-color:inherit">"广东省仙游谷");
  96. "age",0); background-color:inherit">"198");
  97. Stringjson=JSON.toJSONString(map,255); background-color:inherit">true);//转成JSON数据
  98. //遍历数组数据
  99. for(Stringkey:map1.keySet()){
  100. System.out.println(key+":"+map1.get(key));
  101. *通过fastjson把Map换成字符串转
  102. voidmap2JSON(){
  103. Mapmap=newHashMap();
  104. Stringjson=JSON.toJSONString(map);
  105. Mapmap1=JSON.parSEObject(json);
  106. for(Objectobj:map.entrySet()){
  107. Map.Entry<String,String>entry=(Map.Entry<String,String>)obj;
  108. System.out.println(entry.getKey()+"--->"+entry.getValue());
  109. }


copy

packageivyy.taobao.com.entity;
  • importjava.io.Serializable;
  • *@Author:liangjl
  • *@Date:2014-12-19
  • *@Description:
  • classStudentimplementsSerializable{
  • privateIntegerage;
  • privateStringsex;
  • privateStringuserName;
  • privateStringbirthday;
  • privateStringaddress;
  • privateStringemail;
  • publicIntegergetAge(){
  • returnage;
  • voidsetAge(Integerage){
  • this.age=age;
  • publicStringgetSex(){
  • returnsex;
  • voidsetSex(Stringsex){
  • this.sex=sex;
  • publicStringgetUserName(){
  • returnuserName;
  • voidsetUserName(StringuserName){
  • this.userName=userName;
  • publicStringgetBirthday(){
  • returnbirthday;
  • voidsetBirthday(Stringbirthday){
  • this.birthday=birthday;
  • publicStringgetAddress(){
  • returnaddress;
  • voidsetAddress(Stringaddress){
  • this.address=address;
  • publicStringgetEmail(){
  • returnemail;
  • voidsetEmail(Stringemail){
  • this.email=email;
  • }
  • 原文链接:https://www.f2er.com/json/288801.html

    猜你在找的Json相关文章