As shown below:
package Demo;import java.util.ArrayList;import java.util.Collection;import java.util.Date;import java.util.HashMap;import java.util.List;import java.util.Vector;import com.alibaba.fastjson.JSON;import com.alibaba.fastjson.JSONArray;import com.alibaba.fastjson.JSONObject;import com.alibaba.fastjson.serializer.SerializerFeature;import entity.Userinfo;/** * fastjson is a JSON parser and generator implemented in Java language with good performance, developed by Alibaba engineers. Main features: * 1. Fast FAST (faster than any other Java-based parsers and generators, including jackson) Powerful (supports ordinary JDK classes including arbitrary Java Beans * 2.Class, Collection, Map, Date or enum) Zero dependency (no dependency on any other class library except JDK) * */public class TestFastJson { public static void main(String[] args) { String json = "{/"name/":/"chenggang/",/"age/":24}"; String arrayAyy = "[[/'Jack Ma',50],null,[/'Ma Huateng',30]]";// Entity2json("zhangsan", 24);// list2Json(); Complexdata();// Deserialization(json);// DateFormate(new Date());// Json2Eetity(json);// String2JSONArray(arrayAyy); } // Entity converted to Json public static void Entity2json(String name, int age) { Userinfo info = new Userinfo(name, age); String str_json = JSON.toJSONString(info); // System.out.println("Entity converted to Json" + str_json); } // list to Json public static void list2Json() { List<Userinfo> list = new ArrayList<Userinfo>(); Userinfo userinfo1 = new Userinfo("lisi", 15); Userinfo userinfo2 = new Userinfo("wangwu", 16); list.add(userinfo1); list.add(userinfo2); String json = JSON.toJSONString(list, true); System.out.println("List collection to json format string:" + json); } // Convert character array to JSon private static void String2JSONArray(String arrayAyy) { JSONArray array = JSONArray.parseArray(arrayAyy); System.out.println("array:" + array); System.out.println("array length: " + array.size()); Collection nuCon = new Vector(); nuCon.add(null); array.removeAll(nuCon); System.out.println("Array: " + array); System.out.println("Array length: " + array.size()); } // Complex data type public static void Complexdata() { HashMap<String, Object> map = new HashMap<String, Object>(); map.put("username", "zhangsan"); map.put("age", 24); map.put("sex", "male"); // map collection HashMap<String, Object> temp = new HashMap<String, Object>(); temp.put("name", "xiaohong"); temp.put("age", "23"); map.put("girlInfo", temp); // list collection List<String> list = new ArrayList<String>(); list.add("Hill climbing"); list.add("Cycling"); list.add("Travel"); map.put("hobby", list); String jsonString = JSON.toJSONString(map); System.out.println("Complex data type:" + jsonString); } public static void Deserialization(String json) { Userinfo userInfo = JSON.parseObject(json, Userinfo.class); System.out.println("Name is:" + userInfo.getName() + ", age is:" + userInfo.getAge()); } // Format date public static void DateFormate(Date date) { System.out.println("Output millisecond value:" + JSON.toJSONString(date)); System.out.println("Default format is:" + JSON.toJSONString(date, SerializerFeature.WriteDateUseDateFormat)); System.out.println("Custom date:" + JSON.toJSONStringWithDateFormat(date, "yyyy-MM-dd", SerializerFeature.WriteDateUseDateFormat)); } // Json converts to entity private static void Json2Eetity(String json) { Userinfo userInfo = JSON.parseObject(json, Userinfo.class); System.out.println("The address of the output object: " + userInfo.toString()); System.out.println("The name of the output object: " + userInfo.getName()); }}The entity classes used in the above demo:
package entity;public class Userinfo { private static final long serialVersionUID = 1L; private String name; private int age; public Userinfo() { super(); } public Userinfo(String name, int age) { super(); this.name = name; this.age = age; } public void setName(String name) { this.name = name; } public String getName() { return name; } public void setAge(int age) { this.age = age; } public int getAge() { return age; }}The above article briefly discusses the common usage methods of fastjson is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.