fastjson的常用使用方法

  1 package Demo;
  2 
  3 import java.util.ArrayList;
  4 import java.util.Collection;
  5 import java.util.Date;
  6 import java.util.HashMap;
  7 import java.util.List;
  8 import java.util.Vector;
  9 
 10 import com.alibaba.fastjson.JSON;
 11 import com.alibaba.fastjson.JSONArray;
 12 import com.alibaba.fastjson.JSONObject;
 13 import com.alibaba.fastjson.serializer.SerializerFeature;
 14 
 15 import entity.Userinfo;
 16 
 17 /**
 18  * fastjson 是一个性能很好的 Java 语言实现的 JSON 解析器和生成器,来自阿里巴巴的工程师开发。 主要特点:
 19  * 1.快速FAST(比其它任何基于Java的解析器和生成器更快,包括jackson) 强大(支持普通JDK类包括任意Java Bean
 20  * 2.Class、Collection、Map、Date或enum) 零依赖(没有依赖其它任何类库除了JDK)
 21  * 
 22  */
 23 public class TestFastJson {
 24 
 25     public static void main(String[] args) {
 26         String json = "{\"name\":\"chenggang\",\"age\":24}";
 27         String arrayAyy = "[[\'马云',50],null,[\'马化腾',30]]";
 28 //        Entity2json("zhangsan", 24);
 29 //        list2Json();
 30         Complexdata();
 31 //        Deserialization(json);
 32 //        DateFormate(new Date());
 33 //        Json2Eetity(json);
 34 //        String2JSONArray(arrayAyy);
 35     }
 36 
 37     // 实体转为Json
 38     public static void Entity2json(String name, int age) {
 39         Userinfo info = new Userinfo(name, age);
 40         String str_json = JSON.toJSONString(info); //
 41         System.out.println("实体转化为Json" + str_json);
 42     }
 43 
 44     // list转Json
 45     public static void list2Json() {
 46         List<Userinfo> list = new ArrayList<Userinfo>();
 47         Userinfo userinfo1 = new Userinfo("lisi", 15);
 48         Userinfo userinfo2 = new Userinfo("wangwu", 16);
 49         list.add(userinfo1);
 50         list.add(userinfo2);
 51         String json = JSON.toJSONString(list, true);
 52         System.out.println("List集合转json格式字符串 :" + json);
 53     }
 54 
 55     // 字符数组转化为JSon
 56     private static void String2JSONArray(String arrayAyy) {
 57         JSONArray array = JSONArray.parseArray(arrayAyy);
 58         System.out.println("数组:" + array);
 59         System.out.println("数组长度: " + array.size());
 60         Collection nuCon = new Vector();
 61         nuCon.add(null);
 62         array.removeAll(nuCon);
 63         System.out.println("数组:" + array);
 64         System.out.println("数组长度: " + array.size());
 65     }
 66 
 67     // 复杂数据类型
 68     public static void Complexdata() {
 69         HashMap<String, Object> map = new HashMap<String, Object>();
 70         map.put("username", "zhangsan");
 71         map.put("age", 24);
 72         map.put("sex", "男");
 73 
 74         // map集合
 75         HashMap<String, Object> temp = new HashMap<String, Object>();
 76         temp.put("name", "xiaohong");
 77         temp.put("age", "23");
 78         map.put("girlInfo", temp);
 79 
 80         // list集合
 81         List<String> list = new ArrayList<String>();
 82         list.add("爬山");
 83         list.add("骑车");
 84         list.add("旅游");
 85         map.put("hobby", list);
 86         String jsonString = JSON.toJSONString(map);
 87         System.out.println("复杂数据类型:" + jsonString);
 88     }
 89 
 90     public static void Deserialization(String json) {
 91         Userinfo userInfo = JSON.parseObject(json, Userinfo.class);
 92         System.out.println("姓名是:" + userInfo.getName() + ", 年龄是:"
 93                 + userInfo.getAge());
 94     }
 95 
 96     // 格式话日期
 97     public static void DateFormate(Date date) {
 98         System.out.println("输出毫秒值:" + JSON.toJSONString(date));
 99         System.out.println("默认格式为:"
100                 + JSON.toJSONString(date,
101                         SerializerFeature.WriteDateUseDateFormat));
102         System.out.println("自定义日期:"
103                 + JSON.toJSONStringWithDateFormat(date, "yyyy-MM-dd",
104                         SerializerFeature.WriteDateUseDateFormat));
105     }
106 
107     // Json转为实体
108     private static void Json2Eetity(String json) {
109         Userinfo userInfo = JSON.parseObject(json, Userinfo.class);
110         System.out.println("输出对象的地址:" + userInfo.toString());
111         System.out.println("输出对象的名字:" + userInfo.getName());
112     }
113 }

 以上Demo所用到的实体类:

 1 package entity;
 2 
 3 public class Userinfo {
 4     private static final long serialVersionUID = 1L;
 5     private String name;
 6     private int age;
 7 
 8     
 9     public Userinfo() {
10         super();
11     }
12 
13     public Userinfo(String name, int age) {
14         super();
15         this.name = name;
16         this.age = age;
17     }
18 
19     public void setName(String name) {
20         this.name = name;
21     }
22 
23     public String getName() {
24         return name;
25     }
26 
27     public void setAge(int age) {
28         this.age = age;
29     }
30 
31     public int getAge() {
32         return age;
33     }
34 }

 

转载于:https://www.cnblogs.com/stayreal/p/4337066.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值