常用的三种json软件的使用
从几个角度比较三种软件
1. json操作
2 反解
3 性能
易用性还没有列出。
可以根据个人喜好进行取舍。
package json; import com.alibaba.fastjson.JSON; import com.fasterxml.jackson.databind.ObjectMapper; import com.google.gson.Gson; import lombok.Data; public class JsonTest { public static void main(String[] args) throws Exception{ testFastjson(); testJackson(); testGson(); } public static Person getPerson(String name, int age){ Person person = new Person(); person.setAge(age); person.setName(name); return person; } public static void testFastjson(){ long startTime = System.currentTimeMillis(); String jsonstr = ""; Person person1 = getPerson("a",1); for (int i =0;i < 100000;i++){ jsonstr = JSON.toJSONString(person1); Person person = JSON.parseObject(jsonstr, Person.class); } System.out.println("fastjson cost:"+(System.currentTimeMillis()-startTime)); System.out.println(jsonstr); } public static void testJackson() throws Exception{ long startTime = System.currentTimeMillis(); String jsonstr = ""; Person person1 = getPerson("a",1); ObjectMapper mapper = new ObjectMapper(); for (int i =0;i < 100000;i++){ jsonstr = mapper.writeValueAsString(person1); Person person = mapper.readValue(jsonstr, Person.class); } System.out.println("jackson cost:"+(System.currentTimeMillis()-startTime)); System.out.println(jsonstr); } public static void testGson(){ long startTime = System.currentTimeMillis(); String jsonstr = ""; Person person1 = getPerson("a",1); Gson gson = new Gson(); for (int i =0;i < 100000;i++){ jsonstr = gson.toJson(person1); Person person = gson.fromJson(jsonstr, Person.class); } System.out.println("gson cost:"+(System.currentTimeMillis()-startTime)); System.out.println(jsonstr); } } @Data class Person{ private String name; private int age; }