- 创建maven工程并引入jar包
<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.8</version> </dependency>
- 创建一个包com.demo.fastjson
- 在这个包下创建一个实体类Entity.java
package com.demo.fastjson; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; public class Entity { private String vString = "vStringhehhehe"; private char vchar = ‘x‘; private byte vbyte = 64; private short vshort = 128; private int vint = 65535; private long vlong = 9999999L; private float vfloat = 12.1f; private double vdouble = 22.203d; private boolean vboolean = false; // private Date vdate = new Date(); private Date dddd = new Date(); private Date vDate = new Date(); private Date v_Date = new Date(); private Object vnull = null; private String[] avString = {"aaa", "bbb", "ccc"}; private int[] avint = {1, 2, 3, 4}; private boolean[] avboolean = {true, false, true, true}; private List<String> listString = new ArrayList<String>(); private Map<String, String> map = new HashMap<String, String>(); private Person person = new Person(); private Person[] avPerson = {new Person(), new Person()}; private List<Person> listPerson = new ArrayList<Person>(); { listString.add("listString1"); listString.add("listString2"); listString.add("listString3"); map.put("x", "s11111x"); map.put("y", "s22222y"); map.put("z", "s33333z"); listPerson.add(new Person()); listPerson.add(new Person()); listPerson.add(new Person()); } public String getvString() { return vString; } public void setvString(String vString) { this.vString = vString; } public char getVchar() { return vchar; } public void setVchar(char vchar) { this.vchar = vchar; } public byte getVbyte() { return vbyte; } public void setVbyte(byte vbyte) { this.vbyte = vbyte; } public short getVshort() { return vshort; } public void setVshort(short vshort) { this.vshort = vshort; } public int getVint() { return vint; } public void setVint(int vint) { this.vint = vint; } public long getVlong() { return vlong; } public void setVlong(long vlong) { this.vlong = vlong; } public float getVfloat() { return vfloat; } public void setVfloat(float vfloat) { this.vfloat = vfloat; } public double getVdouble() { return vdouble; } public void setVdouble(double vdouble) { this.vdouble = vdouble; } public boolean isVboolean() { return vboolean; } public void setVboolean(boolean vboolean) { this.vboolean = vboolean; } public Date getDddd() { return dddd; } public void setDddd(Date dddd) { this.dddd = dddd; } public Date getV_Date() { return v_Date; } public void setV_Date(Date v_Date) { this.v_Date = v_Date; } public Date getvDate() { return vDate; } public void setvDate(Date vDate) { this.vDate = vDate; } public Object getVnull() { return vnull; } public void setVnull(Object vnull) { this.vnull = vnull; } public String[] getAvString() { return avString; } public void setAvString(String[] avString) { this.avString = avString; } public int[] getAvint() { return avint; } public void setAvint(int[] avint) { this.avint = avint; } public boolean[] getAvboolean() { return avboolean; } public void setAvboolean(boolean[] avboolean) { this.avboolean = avboolean; } public List<String> getListString() { return listString; } public void setListString(List<String> listString) { this.listString = listString; } public Map<String, String> getMap() { return map; } public void setMap(Map<String, String> map) { this.map = map; } public Person getPerson() { return person; } public void setPerson(Person person) { this.person = person; } public Person[] getAvPerson() { return avPerson; } public void setAvPerson(Person[] avPerson) { this.avPerson = avPerson; } public List<Person> getListPerson() { return listPerson; } public void setListPerson(List<Person> listPerson) { this.listPerson = listPerson; } }
Entity.java代码
- 还是这个包下,创建一个Person.java
package com.demo.fastjson; import java.util.Date; import java.util.Random; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.serializer.SerializeConfig; import com.alibaba.fastjson.serializer.SimpleDateFormatSerializer; public class Person { public static SerializeConfig mapping = new SerializeConfig(); private String personName; private int personAge; private Date personDate = new Date(); static { mapping.put(Date.class, new SimpleDateFormatSerializer("yyyy-MM-dd")); } { Random r = new Random(); personName = "sss_"+String.valueOf(r.nextFloat()); personAge = r.nextInt(); } public static void main(String[] args) { Object obj = JSON.toJSON(new Person()); String x1 = JSON.toJSONString(new Person(), true); System.out.println(x1); String x2 = JSON.toJSONString(new Person(), mapping); System.out.println(x2); } public String getPersonName() { return personName; } public void setPersonName(String PersonName) { this.personName = PersonName; } public int getPersonAge() { return personAge; } public void setPersonAge(int personAge) { this.personAge = personAge; } public Date getzPersonDate() { return personDate; } public void setPersonDate(Date personDate) { this.personDate = personDate; } @Override public String toString() { return "Person{" + "personName=‘" + personName + ‘\‘‘ + ", personAge=" + personAge + ", personDate=" + personDate + ‘}‘; } }
Person.java代码
- 还在这个包下,创建一个TestFastjson.java
package com.demo.fastjson; import java.util.ArrayList; import java.util.Date; 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.serializer.SerializeConfig; import com.alibaba.fastjson.serializer.SimpleDateFormatSerializer; public class TestFastjson { private static SerializeConfig mapping = new SerializeConfig(); static { mapping.put(Date.class, new SimpleDateFormatSerializer("yyyy-MM-dd HH:mm:ss")); } public static void main(String[] args) { Entity entity = new Entity(); Date date = new Date(); String text = JSON.toJSONString(date, mapping); System.out.println(text); System.out.println(JSON.toJSONString(entity, true)); String x2 = JSON.toJSONString(entity, mapping); System.out.println(x2); } public static void json2List() { // List -> JSON array List<Person> PersonList = new ArrayList<Person>(); PersonList.add(new Person()); PersonList.add(new Person()); PersonList.add(new Person()); String json = JSON.toJSONString(PersonList, true); System.out.println(json); // JSON array -> List List<Person> personList1 = JSON.parseArray(json, Person.class); for (Person person : personList1) { System.out.println(person.toString()); } } public static void json2Map() { // Map -> JSON Map<String, Person> map = new HashMap<String, Person>(); map.put("a", new Person()); map.put("b", new Person()); map.put("c", new Person()); String json = JSON.toJSONString(map, true); System.out.println(json); // JSON -> Map Map<String, Person> map1 = (Map<String, Person>) JSON.parse(json); for (String key : map1.keySet()) { System.out.println(key + ":" + map1.get(key)); } } public static void array2JSON() { String[] arr_String = { "a", "b", "c" }; String json_arr_String = JSON.toJSONString(arr_String, true); System.out.println(json_arr_String); JSONArray jsonArray = JSON.parseArray(json_arr_String); for (Object o : jsonArray) { System.out.println(o); } System.out.println(jsonArray); } public static void array2JSON2() { Person[] arr_Person = { new Person(), new Person(), new Person() }; String json_arr_Person = JSON.toJSONString(arr_Person, true); System.out.println(json_arr_Person); JSONArray jsonArray = JSON.parseArray(json_arr_Person); for (Object o : jsonArray) { System.out.println(o); } System.out.println(jsonArray); } public static void map2JSON() { Map map = new HashMap(); map.put("a", "aaa"); map.put("b", "bbb"); map.put("c", "ccc"); String json = JSON.toJSONString(map); System.out.println(json); Map map1 = JSON.parseObject(json); for (Object o : map.entrySet()) { Map.Entry<String, String> entry = (Map.Entry<String, String>) o; System.out.println(entry.getKey() + "--->" + entry.getValue()); } } }
TestFastjson.java代码
- Person.java运行结果并分析
//第一次输出一个Person类对象的json字符串 //第二个实参是一个布尔值true,形参变量名为prettyFormat,输出格式可读性强 { "personAge":-1558299568, "personName":"sss_0.2234999", "zPersonDate":1492426894572 } //第二次输出的还是一个Person类对象的json字符串 //第二个实参是一个自定义的格式,日期会按自定义格式输出 {"personAge":-1538580665,"personName":"sss_0.96627986","zPersonDate":"2017-04-17"}
- TestFastjson运行结果并分析
//第一次输出一个自定义格式的日期 "2017-04-17 19:17:55" //第二次输出一个Entity类对象的json 字符串,可读性强 //这是一个java 对象,对应json 字符串的对象 { //这是这个java 对象里的第一个属性Person数组 "avPerson":[{ "personAge":408324610, "personName":"sss_0.31633854", "zPersonDate":1492427875498 },{ "personAge":-32783373, "personName":"sss_0.31466597", "zPersonDate":1492427875498 }], //这是这个java 对象里的第二个属性字符串数组 "avString":["aaa","bbb","ccc"], //这是这个java 对象里的第三个属性布尔值数组 "avboolean":[true,false,true,true], //这是这个java 对象里的第四个属性int数组 "avint":[1,2,3,4], //这是这个java 对象里的第五个日期属性long数值 "dddd":1492427875496, //这是这个java 对象里的第六个属性Person集合 "listPerson":[ { "personAge":-1130093579, "personName":"sss_0.80541015", "zPersonDate":1492427875498 }, { "personAge":-1257501011, "personName":"sss_0.9102172", "zPersonDate":1492427875498 }, { "personAge":1886596126, "personName":"sss_0.9991592", "zPersonDate":1492427875498 } ], //这是这个java 对象里的第七个属性Sting集合 "listString":[ "listString1", "listString2", "listString3" ], //这是这个java 对象里的第八个属性map对象 "map":{ "x":"s11111x", "y":"s22222y", "z":"s33333z" }, //这是这个java 对象里的第九个属性Person对象 "person":{ "personAge":50986186, "personName":"sss_0.77795047", "zPersonDate":1492427875496 }, //这是这个java 对象里的第十个属性日期对象的long值 "vDate":1492427875496, //这是这个java 对象里的第十一个属性字符串对象 "vString":"vStringhehhehe", //这是这个java 对象里的第十二个属性日期类的long值 "v_Date":1492427875496, //这是这个java 对象里的第十三个属性布尔值 "vboolean":false, //这是这个java 对象里的第十四个属性byte 数值 "vbyte":64, //这是这个java 对象里的第十五个属性char 字符 "vchar":"x", //这是这个java 对象里的第十六个属性double 数值 "vdouble":22.203, //这是这个java 对象里的第十七个属性float 数值 "vfloat":12.1, //这是这个java 对象里的第十八个属性int 数值 "vint":65535, //这是这个java 对象里的第十九个属性long 数值 "vlong":9999999, //这是这个java 对象里的第二十个属性short 数值 "vshort":128 } //第三次还是输出一个Entity类对象的json 字符串,可读性差 {"avPerson":[{"personAge":408324610,"personName":"sss_0.31633854","zPersonDate":"2017-04-17 19:17:55"},{"personAge":-32783373,"personName":"sss_0.31466597","zPersonDate":"2017-04-17 19:17:55"}],"avString":["aaa","bbb","ccc"],"avboolean":[true,false,true,true],"avint":[1,2,3,4],"dddd":"2017-04-17 19:17:55","listPerson":[{"personAge":-1130093579,"personName":"sss_0.80541015","zPersonDate":"2017-04-17 19:17:55"},{"personAge":-1257501011,"personName":"sss_0.9102172","zPersonDate":"2017-04-17 19:17:55"},{"personAge":1886596126,"personName":"sss_0.9991592","zPersonDate":"2017-04-17 19:17:55"}],"listString":["listString1","listString2","listString3"],"map":{"x":"s11111x","y":"s22222y","z":"s33333z"},"person":{"personAge":50986186,"personName":"sss_0.77795047","zPersonDate":"2017-04-17 19:17:55"},"vDate":"2017-04-17 19:17:55","vString":"vStringhehhehe","v_Date":"2017-04-17 19:17:55","vboolean":false,"vbyte":64,"vchar":"x","vdouble":22.203,"vfloat":12.1,"vint":65535,"vlong":9999999,"vshort":128}
时间: 2024-11-05 00:52:26