通过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

[java] view plain copy
 print?
  1. package ivyy.taobao.com.domain.fastjson;  
  2.   
  3. import ivyy.taobao.com.entity.Student;  
  4.   
  5. import java.util.ArrayList;  
  6. import java.util.Arrays;  
  7. import java.util.HashMap;  
  8. import java.util.List;  
  9. import java.util.Map;  
  10.   
  11. import com.alibaba.fastjson.JSON;  
  12. import com.alibaba.fastjson.JSONObject;  
  13. import com.alibaba.fastjson.TypeReference;  
  14.   
  15. /** 
  16.  *@DEMO:json 
  17.  *@Java:FastJSON.java 
  18.  *@Date:2015-1-19上午10:28:12 
  19.  *@Author:龙叔 
  20.  *@Email:jilongliang@sina.com 
  21.  *@Weibo:http://weibo.com/jilongliang 
  22.  *@Version:1.0 
  23.  *@Description:fastjson跟json-lib是语法很像,一句话说,所有json都差不多, 
  24.  *大家伙也没不要研究那么多,懂一种自己最擅长而且熟悉能解决自己要解决的问题就OK, 
  25.  *从fastjson反编译过来看 你就看到pom.xml里面的配置肯定能看到json-lib,此时能 
  26.  *证明fastjson就是运用了json-lib! 
  27.  * 
  28.  *-------------------------------------------- 
  29.  *      <dependency> 
  30.             <groupId>org.codehaus.jackson</groupId> 
  31.             <artifactId>jackson-smile</artifactId> 
  32.             <version>1.9.9</version> 
  33.             <scope>test</scope> 
  34.         </dependency> 
  35. *-------------------------------------------- 
  36.         <dependency> 
  37.             <groupId>com.googlecode.json-simple</groupId> 
  38.             <artifactId>json-simple</artifactId> 
  39.             <version>1.1</version> 
  40.             <scope>test</scope> 
  41.         </dependency> 
  42. -------------------------------------------- 
  43. *        <dependency> 
  44.             <groupId>net.sf.json-lib</groupId> 
  45.             <artifactId>json-lib</artifactId> 
  46.             <version>2.4</version> 
  47.             <classifier>jdk15</classifier> 
  48.             <scope>test</scope> 
  49.         </dependency> 
  50. -------------------------------------------- 
  51.  */  
  52. public class FastJSON {  
  53.   
  54.     /** 
  55.      * @param args 
  56.      */  
  57.     public static void main(String[] args) throws Exception{  
  58.         //string2Json();  
  59.         //string2Object();  
  60.         //string2List();  
  61.           
  62.         map2json();  
  63.         map2JSON();  
  64.     }  
  65.       
  66.       
  67.     /** 
  68.      * 通过fastjson把字符串转换成JSON数据 
  69.      * TypeReference 
  70.      */  
  71.     public static void string2Json(){  
  72.         StringBuffer buffer=new StringBuffer();  
  73.         buffer.append("{");  
  74.             buffer.append("\"age\":").append("27").append(",");  
  75.             buffer.append("\"userName\":").append("\"龙叔\"").append(",");  
  76.             buffer.append("\"address\":").append("\"广东省云浮市\"");  
  77.         buffer.append("}");  
  78.           
  79.         String jsonText=buffer.toString();  
  80.           
  81.         JSONObject jobj=JSON.parseObject(jsonText);  
  82.         String address=jobj.get("address").toString();  
  83.         System.out.println(address);  
  84.     }  
  85.       
  86.       
  87.     /** 
  88.      * 通过fastjson把字符串转换成对象 
  89.      * TypeReference 
  90.      */  
  91.     public static void string2Object(){  
  92.         StringBuffer buffer=new StringBuffer();  
  93.         buffer.append("{");  
  94.             buffer.append("\"age\":").append("27").append(",");  
  95.             buffer.append("\"userName\":").append("\"龙叔\"").append(",");  
  96.             buffer.append("\"address\":").append("\"广东省云浮市\"");  
  97.         buffer.append("}");  
  98.           
  99.         String jsonText=buffer.toString();  
  100.         //方法一 把json字符串转成Student对象  
  101.         Student stu1 = JSON.parseObject(jsonText, new TypeReference<Student>(){});  
  102.         //方法二 把json字符串转成Student对象  
  103.         Student stu2 = JSON.parseObject(jsonText,Student.class);    
  104.           
  105.         System.out.println(stu1.getAddress());  
  106.         System.out.println(stu2.getAddress());  
  107.     }  
  108.       
  109.     /** 
  110.      * 通过fastjson把字符串转换成泛型数组 
  111.      * TypeReference 
  112.      */  
  113.     public static void string2List(){  
  114.         StringBuffer buffer=new StringBuffer();  
  115.         buffer.append("[{");  
  116.             buffer.append("\"age\":").append("27").append(",");  
  117.             buffer.append("\"userName\":").append("\"龙叔\"").append(",");  
  118.             buffer.append("\"address\":").append("\"广东省云浮市\"");  
  119.         buffer.append("}]");  
  120.           
  121.         String jsonText=buffer.toString();  
  122.         //转成成数组  
  123.         Student[] stu2 = JSON.parseObject(jsonText,new TypeReference<Student[]>(){});    
  124.         List<Student> list = Arrays.asList(stu2);  
  125.           
  126.         for(Student st:list){  
  127.             System.out.println(st.getAddress());  
  128.         }  
  129.           
  130.         // 转换成ArrayList  
  131.         ArrayList<Student> list2 = JSON.parseObject(jsonText, new TypeReference<ArrayList<Student>>(){});   
  132.           
  133.         for (int i = 0; i < list2.size(); i++) {  
  134.             Student obj =(Student) list2.get(i);  
  135.             System.out.println(obj.getAddress());  
  136.         }  
  137.           
  138.     }  
  139.     /** 
  140.      * 通过fastjson把Map换成字符串转 
  141.      */  
  142.     public static void map2json(){  
  143.         //创建一个Map对象  
  144.          Map<String,String> map = new HashMap<String, String>();  
  145.          map.put("username""周伯通");  
  146.          map.put("address""广东省仙游谷");  
  147.          map.put("age""198");  
  148.          String json = JSON.toJSONString(map,true); //转成JSON数据  
  149.            
  150.          Map<String,String> map1 = (Map<String,String>)JSON.parse(json);   
  151.          //遍历数组数据  
  152.          for (String key : map1.keySet()) {   
  153.             System.out.println(key+":"+map1.get(key));   
  154.         }   
  155.     }  
  156.     /** 
  157.      * 通过fastjson把Map换成字符串转 
  158.      */  
  159.     public static void map2JSON() {  
  160.         Map map = new HashMap();  
  161.         map.put("username""周伯通");  
  162.         map.put("address""广东省仙游谷");  
  163.         map.put("age""198");  
  164.         String json = JSON.toJSONString(map);  
  165.         Map map1 = JSON.parseObject(json);  
  166.         for (Object obj : map.entrySet()) {  
  167.             Map.Entry<String, String> entry = (Map.Entry<String, String>) obj;  
  168.             System.out.println(entry.getKey() + "--->" + entry.getValue());  
  169.         }  
  170.     }   
  171. }  


[java] view plain copy
 print?
  1. package ivyy.taobao.com.entity;  
  2.   
  3. import java.io.Serializable;  
  4.   
  5. /** 
  6.  *@Author:liangjl 
  7.  *@Date:2014-12-19 
  8.  *@Version:1.0 
  9.  *@Description: 
  10.  */  
  11. public class Student implements Serializable{  
  12.     private Integer age;  
  13.     private String sex;  
  14.     private String userName;  
  15.     private String birthday;  
  16.     private String address;  
  17.     private String email;  
  18.       
  19.     public Integer getAge() {  
  20.         return age;  
  21.     }  
  22.     public void setAge(Integer age) {  
  23.         this.age = age;  
  24.     }  
  25.     public String getSex() {  
  26.         return sex;  
  27.     }  
  28.     public void setSex(String sex) {  
  29.         this.sex = sex;  
  30.     }  
  31.     public String getUserName() {  
  32.         return userName;  
  33.     }  
  34.     public void setUserName(String userName) {  
  35.         this.userName = userName;  
  36.     }  
  37.     public String getBirthday() {  
  38.         return birthday;  
  39.     }  
  40.     public void setBirthday(String birthday) {  
  41.         this.birthday = birthday;  
  42.     }  
  43.     public String getAddress() {  
  44.         return address;  
  45.     }  
  46.     public void setAddress(String address) {  
  47.         this.address = address;  
  48.     }  
  49.     public String getEmail() {  
  50.         return email;  
  51.     }  
  52.     public void setEmail(String email) {  
  53.         this.email = email;  
  54.     }  
  55. }  


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值