脚本宝典收集整理的这篇文章主要介绍了javascript代码实例教程-JSON之FastJson,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。小宝典致力于为广大程序猿(媛)提供高品质的代码服务,请大家多多光顾小站,小宝典在此谢过。 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{
12 private int age;
13 public void setName(String name){
14 this.name=name;
15 }
16 public String getName(){
17 return name;
18 }
19 public void setAge(int age){
20 this.age=age;
21 }
22 public int getAge(){
23 return 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 }
32 public static void test2() {
33
34 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 }
55 public static void 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 /**泛型的反序列化*/
62 public static void 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 }
67 public static void 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 }
76 public static void 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
4 test.UserInfo@30c221
5 1408190003876
6 "2014-08-16 19:53:23"
7 "2014-08-16"
觉得可用,就经常来吧! 脚本宝典 欢迎评论哦! js脚本,巧夺天工,精雕玉琢。小宝典献丑了!
以上是脚本宝典为你收集整理的javascript代码实例教程-JSON之FastJson全部内容,希望文章能够帮你解决javascript代码实例教程-JSON之FastJson所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。