首页 / JSON / JSON之FastJson
JSON之FastJson
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了JSON之FastJson,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3821字,纯文字阅读大概需要6分钟。
内容图文
FastJson是什么?
从网上查到---
官网地址:http://code.alibabatech.com/wiki/display/FastJSON/Overview(已关闭)
FastJSOn是阿里巴巴开源的JSON处理工具,包括“序列化”和“反序列化”两部分,它具备如下特征:
- 速度最快,测试表明,fastjson具有极快的性能,超越任其他的java json parser。包括自称最快的jackson。
- 功能强大,完全支持java bean、集合、Map、日期、Enum,支持范型,支持自省。
- 无依赖,能够直接运行在Java SE 5.0以上版本
- 支持Android。
- 开源 (Apache 2.0)
既然速度快,当然要体验一下。更何况是国产货,当然要支持了。测试一下,以后在项目里使用...
1 package test; 2 import java.io.Serializable; 3 /** 4 * <一句话功能简述> 5 * @author aliger Email:liqimo#gmail.com 6 * @version [V1.00, 2014-8-16] 7 * @see [相关类/方法] 8 * @since V1.00 9 */ 10 public class UserInfo implements Serializable{ 11private String name; 12privateint age; 13publicvoid setName(String name){ 14this.name=name; 15 } 16public String getName(){ 17return name; 18 } 19publicvoid setAge(int age){ 20this.age=age; 21 } 22publicint getAge(){ 23return age; 24 } 25 }
Main:
1 package test; 2 import java.util.ArrayList; 3 import java.util.Date; 4 import java.util.HashMap; 5 import java.util.List; 6 import java.util.Map; 7 import com.alibaba.fastjson.JSON; 8 import com.alibaba.fastjson.TypeReference; 9 import com.alibaba.fastjson.serializer.SerializerFeature; 10 /** 11 * <一句话功能简述> 12 * 13 * @author aliger Email:liqimo#gmail.com 14 * @version [V1.00, 2014-8-16] 15 * @see [相关类/方法] 16 * @since V1.00 17 */ 18 public class TestOne 19 { 20 public static FastJsonUtils ff; 21 22 // public static void main(String[] args){ 23 public static void test1(){ 24 UserInfo info=new UserInfo(); 25 info.setName("zhangsan"); 26 info.setAge(24); 27//将对象转换为JSON字符串 28//String str_json=JSON.toJSONString(info); 29 String str_json=ff.getJsonString(info); 30 System.out.println("JSON="+str_json); 31 } 32publicstaticvoid test2() { 3334 HashMap<String, Object> map = new HashMap<String, Object>(); 35 map.put("username", "zhangsan"); 36 map.put("age", 24); 37 map.put("sex", "男"); 38//map集合 39 HashMap<String, Object> temp = new HashMap<String, Object>(); 40 temp.put("name", "xiaohong"); 41 temp.put("age", "23"); 42 map.put("girlInfo", temp); 43//list集合 44 List<String> list = new ArrayList<String>(); 45 list.add("爬山"); 46 list.add("骑车"); 47 list.add("旅游"); 48 map.put("hobby", list); 49/*JSON 序列化,默认序列化出的JSON字符串中键值对是使用双引号,如果需要单引号的JSON字符串, [eg:String jsonString = JSON.toJSONString(map, SerializerFeature.UseSingleQuotes);] 50 *fastjson序列化时可以选择的SerializerFeature有十几个属性,你可以按照自己的需要去选择使用。 51*/52 String jsonString = JSON.toJSONString(map); 53 System.out.println("JSON===" + jsonString); 54 } 55publicstaticvoid test3(){ 56 String json="{\"name\":\"chenggang\",\"age\":24}"; 57//反序列化 58 UserInfo userInfo=JSON.parseObject(json,UserInfo.class); 59 System.out.println("name:"+userInfo.getName()+", age:"+userInfo.getAge()); 60 } 61/**泛型的反序列化*/62publicstaticvoid test4(){ 63 String json="{\"user\":{\"name\":\"zhangsan\",\"age\":25}}"; 64 Map<String, UserInfo> map = JSON.parseObject(json, new TypeReference<Map<String, UserInfo>>(){}); 65 System.out.println(map.get("user")); 66 } 67publicstaticvoid test5(){ 68 Date date=new Date(); 69//输出毫秒值 70 System.out.println(JSON.toJSONString(date)); 71//默认格式为yyyy-MM-dd HH:mm:ss 72 System.out.println(JSON.toJSONString(date, SerializerFeature.WriteDateUseDateFormat)); 73//根据自定义格式输出日期 74 System.out.println(JSON.toJSONStringWithDateFormat(date, "yyyy-MM-dd", SerializerFeature.WriteDateUseDateFormat)); 75 } 76publicstaticvoid main(String[] args) { 77 test1(); 78 test2(); 79 test3(); 80 test4(); 81 test5(); 82 } 83 }
out结果:
1 JSON={"age":24,"name":"zhangsan"} 2 JSON==={"age":24,"girlInfo":{"age":"23","name":"xiaohong"},"hobby":["爬山","骑车","旅游"],"sex":"男","username":"zhangsan"} 3 name:chenggang, age:24 4test.UserInfo@30c221 5 1408190003876 6 "2014-08-16 19:53:23" 7 "2014-08-16"
原文:http://www.cnblogs.com/aliger/p/3916848.html
内容总结
以上是互联网集市为您收集整理的JSON之FastJson全部内容,希望文章能够帮你解决JSON之FastJson所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。