Java操作JSON数据(3)--fastjson操作JSON数据
程序员文章站
2022-06-22 10:29:55
fastjson是阿里巴巴的开源JSON解析库,它可以解析JSON格式的字符串,支持将Java Bean序列化为JSON字符串,也可以从JSON字符串反序列化到JavaBean。本介绍下fastjson的基本使用方法,包括序列化和反序列化;文中所使用到的软件版本:Java 1.8.0_191、Gso ......
fastjson是阿里巴巴的开源json解析库,它可以解析json格式的字符串,支持将java bean序列化为json字符串,也可以从json字符串反序列化到javabean。本介绍下fastjson的基本使用方法,包括序列化和反序列化;文中所使用到的软件版本:java 1.8.0_191、gson 1.2.62。
1、引入依赖
<dependency> <groupid>com.alibaba</groupid> <artifactid>fastjson</artifactid> <version>1.2.62</version> </dependency>
2、序列化
public static string serialize() { mybean bean = new mybean(); bean.sets("测试字符串"); calendar calendar = calendar.getinstance(); calendar.add(calendar.hour_of_day, -1); bean.setd(calendar.gettime()); list<mybean> list = new arraylist<mybean>(); list.add(bean); map<string, list<mybean>> map = new hashmap<string, list<mybean>>(); map.put("data", list); /* * "yyyy-mm-dd hh:mm:ss" 设置日期格式,也可在类属性上使用@jsonfield(format="yyyy-mm-dd hh:mm:ss")注解来设置 * serializerfeature.writemapnullvalue 输出值为null的字段 * serializerfeature.writenullstringasempty 字符串如果为null则输出"" */ string result = json.tojsonstringwithdateformat(map, "yyyy-mm-dd hh:mm:ss", serializerfeature.writemapnullvalue, serializerfeature.writenullstringasempty); system.out.println(result); return result; }
3、反序列化
public static void deserialize() { string s = serialize(); typereference<map<string, list<mybean>>> type = new typereference<map<string, list<mybean>>>(){}; map<string, list<mybean>> map = json.parseobject(s, type); system.out.println(map); //直接通过api获取相应数据 jsonobject jsonobject = json.parseobject(s); jsonarray jsonarray = jsonobject.getjsonarray("data"); jsonobject bean = jsonarray.getjsonobject(0); system.out.println(bean.getstring("s")); //... }
4、完整例子
package com.inspur.demo.json; import java.util.arraylist; import java.util.calendar; import java.util.hashmap; import java.util.list; import java.util.map; import com.alibaba.fastjson.json; import com.alibaba.fastjson.jsonarray; import com.alibaba.fastjson.jsonobject; import com.alibaba.fastjson.typereference; import com.alibaba.fastjson.serializer.serializerfeature; /** * fastjson操作json数据 */ public class fastjsoncase { /** * 序列化 */ public static string serialize() { mybean bean = new mybean(); bean.sets("测试字符串"); calendar calendar = calendar.getinstance(); calendar.add(calendar.hour_of_day, -1); bean.setd(calendar.gettime()); list<mybean> list = new arraylist<mybean>(); list.add(bean); map<string, list<mybean>> map = new hashmap<string, list<mybean>>(); map.put("data", list); /* * "yyyy-mm-dd hh:mm:ss" 设置日期格式,也可在类属性上使用@jsonfield(format="yyyy-mm-dd hh:mm:ss")注解来设置 * serializerfeature.writemapnullvalue 输出值为null的字段 * serializerfeature.writenullstringasempty 字符串如果为null则输出"" */ string result = json.tojsonstringwithdateformat(map, "yyyy-mm-dd hh:mm:ss", serializerfeature.writemapnullvalue, serializerfeature.writenullstringasempty); system.out.println(result); return result; } /** * 反序列化 */ public static void deserialize() { string s = serialize(); typereference<map<string, list<mybean>>> type = new typereference<map<string, list<mybean>>>(){}; map<string, list<mybean>> map = json.parseobject(s, type); system.out.println(map); //直接通过api获取相应数据 jsonobject jsonobject = json.parseobject(s); jsonarray jsonarray = jsonobject.getjsonarray("data"); jsonobject bean = jsonarray.getjsonobject(0); system.out.println(bean.getstring("s")); //... } public static void main(string[] args) { serialize(); deserialize(); } }
package com.inspur.demo.json; import java.util.arrays; import java.util.date; public class mybean { private string s = "字符串"; private string s2; private string s3 = "字符串3"; private int i = 123; private integer i2; private char c = '到'; private char[] c2 = new char[]{'a', 'b'}; private date d = new date(); private date[] d2 = new date[]{new date(), new date()}; public string gets() { return s; } public void sets(string s) { this.s = s; } public string gets2() { return s2; } public void sets2(string s2) { this.s2 = s2; } public string gets3() { return s3; } public void sets3(string s3) { this.s3 = s3; } public int geti() { return i; } public void seti(int i) { this.i = i; } public integer geti2() { return i2; } public void seti2(integer i2) { this.i2 = i2; } public char getc() { return c; } public void setc(char c) { this.c = c; } public char[] getc2() { return c2; } public void setc2(char[] c2) { this.c2 = c2; } public date getd() { return d; } public void setd(date d) { this.d = d; } public date[] getd2() { return d2; } public void setd2(date[] d2) { this.d2 = d2; } @override public string tostring() { return "mybean [s=" + s + ", s2=" + s2 + ", s3=" + s3 + ", i=" + i + ", i2=" + i2 + ", c=" + c + ", c2=" + arrays.tostring(c2) + ", d=" + d + ", d2=" + arrays.tostring(d2) + "]"; } }