DBMNG数据库管理与应用

所谓独创的能力,就是经过深思的模仿。
当前位置:首页 > 经验分享 > Java组件

FastJson简单使用

在工作中,经常客服端需要和服务端进行通信,目前很多项目都采用JSON的方式进行数据传输,简单的参数可以通过手动拼接JSON字符串,但如果请求的参数过多,采用手动拼接JSON字符串,出错率就非常大了。并且工作效率也特别低。

我在网上看了一些开源的JSON框架,比如Google提供的Gson,Jackson,FastJson等框架。

经过测试,个人觉得FastJson执行效率比较高,而且简单易用。
FastJson不依赖于第三方包, 直接可以运行在Java JDK1.5之上,FastJson完全支持http://json.org的标准,支持各种JDK类型,包括基本类型、JavaBean、Collection、Map、Enum、泛型等
还支持循环引用。
FastJson项目是开源的:Fastjson代码托管在github.org上,项目地址是 https://github.com/AlibabaTech/fastjson

一个JSON库涉及的最基本功能就是序列化和反序列化。Fastjson支持java bean的直接序列化。使用com.alibaba.fastjson.JSON这个类进行序列化和反序列化。

 

一。简单的序列化

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
pubic classUserInfo implementsSerializable{ 
 privateString name; 
 privateintage; 
 publicvoidsetName(String name){ 
  this.name=name; 
 } 
 publicString getName(){ 
  returnname; 
 } 
    
 publicvoidsetAge(intage){ 
  this.age=age; 
 } 
 publicintgetAge(){ 
  returnage; 
 } 
}  
   
publicclassTestOne{ 
    
 publicstaticvoidmain(String[] args){ 
  UserInfo info=newUserInfo(); 
  info.setName("zhangsan"); 
  info.setAge(24); 
  //将对象转换为JSON字符串 
  String str_json=JSON.toJSONString(info); 
  System.out.println("JSON="+str_json); 
 } 
   
}

二.复杂的数据类型

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
publicstaticvoidtest2() { 
   
  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); 
     
  /*JSON 序列化,默认序列化出的JSON字符串中键值对是使用双引号,如果需要单引号的JSON字符串, [eg:String jsonString = JSON.toJSONString(map,   SerializerFeature.UseSingleQuotes);]
   *fastjson序列化时可以选择的SerializerFeature有十几个属性,你可以按照自己的需要去选择使用。 
   */ 
  String jsonString = JSON.toJSONString(map); 
  System.out.println("JSON="+ jsonString); 
   
}

三.反序列化

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
publicvoidtest3(){ 
   
 String json="{\"name\":\"chenggang\",\"age\":24}"; 
 //反序列化 
 UserInfo userInfo=JSON.parseObject(json,UserInfo.class); 
 System.out.println("name:"+userInfo.getName()+", age:"+userInfo.getAge()); 
    
/**泛型的反序列化*/ 
publicstaticvoidtest4(){ 
  String json="{\"user\":{\"name\":\"zhangsan\",\"age\":25}}"; 
  Map<String, UserInfoBean> map = JSON.parseObject(json, newTypeReference<Map<String, UserInfoBean>>(){}); 
  System.out.println(map.get("user")); 
 }

//同理, json字符串中可以嵌套各种数据类型。

 

四.日期格式化

?
1
2
3
4
5
6
7
8
9
10
11
publicvoidtest5(){ 
   
  Date date=newDate();   
  //输出毫秒值 
  System.out.println(JSON.toJSONString(date)); 
  //默认格式为yyyy-MM-dd HH:mm:ss   
  System.out.println(JSON.toJSONString(date, SerializerFeature.WriteDateUseDateFormat)); 
  //根据自定义格式输出日期  
  System.out.println(JSON.toJSONStringWithDateFormat(date, "yyyy-MM-dd", SerializerFeature.WriteDateUseDateFormat)); 
   
}
本站文章内容,部分来自于互联网,若侵犯了您的权益,请致邮件chuanghui423#sohu.com(请将#换为@)联系,我们会尽快核实后删除。
Copyright © 2006-2023 DBMNG.COM All Rights Reserved. Powered by DEVSOARTECH            豫ICP备11002312号-2

豫公网安备 41010502002439号