FASTJSON2是FASTJSON项目的重要升级,目标是为下一个十年提供一个高性能的JSON库。
官方如是说。
查看新版本: maven.org 。
<dependency>
<groupId>com.alibaba.fastjson2</groupId>
<artifactId>fastjson2</artifactId>
<version>2.0.12</version>
</dependency>
如果原来使用fastjson 1.2.x
版本,可以使用兼容包,兼容包不能保证100%兼容,请仔细测试验证,发现问题请及时反馈。
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>2.0.14</version>
</dependency>
在另一篇文章里有,直接取来用。
[
{"name": "张三", "age": 10, "score": 23, "groupTag": "A"},
{"name": "李四", "age": 20, "score": 19, "groupTag": "A"},
{"name": "王五", "age": 30, "score": 20, "groupTag": "B"},
{"name": "赵六", "age": 40, "score": 21, "groupTag": "B"},
{"name": "洪七", "age": 50, "score": 25, "groupTag": "C"},
{"name": "重八", "age": 60, "score": 45, "groupTag": "C"}
]
Hero hero = Hero.getList().get(0);
String jsonString = JSONObject.toJSONString(hero);
{"age":10,"groupTag":"A","name":"张三","score":23}
更多详情,见参考资料:通过Features配置序列化和反序列化的行为
Hero hero = Hero.getList().get(0);
String jsonString = JSON.toJSONString(hero, JSONWriter.Feature.PrettyFormat);
{
"age":10,
"groupTag":"A",
"name":"张三",
"score":23
}
Hero hero = Hero.getList().get(0);
String jsonString = JSON.toJSONString(hero, JSONWriter.Feature.WriteClassName);
{
"@type": "com.jerry.demo.fastjson2demo.entity.Hero",
"age": 10,
"groupTag": "A",
"name": "张三",
"score": 23
}
JSONWriter.Feature.UseSingleQuotes
Hero hero = Hero.getList().get(0);
String jsonString = JSONObject.toJSONString(hero,
JSONWriter.Feature.PrettyFormat, // 格式化输出
JSONWriter.Feature.UseSingleQuotes); // 使用单引号
{ "age":10, 'groupTag':null, 'name':'张三', "score":23 }
JSONWriter.Feature.WriteNulls
Hero hero = Hero.getList().get(0);
hero.setGroupTag(null);
String jsonString = JSONObject.toJSONString(hero,
JSONWriter.Feature.PrettyFormat, // 格式化输出
JSONWriter.Feature.UseSingleQuotes, // 使用单引号
JSONWriter.Feature.WriteNulls); // 序列话时包含为 null 的字段
{ "age":10, 'groupTag':null, 'name':'张三', "score":23 }
String jsonString = "{ 'age':10,'groupTag':'A','name':'张三','score':23 }";
Hero hero = JSONObject.parseObject(jsonString, Hero.class);
String jsonString = "{ 'age':10,'groupTag':'A','name':'张三','score':23 }";
JSONObject jsonObject = JSONObject.parseObject(jsonString);
int id = jsonObject.getIntValue("age");
String name = jsonObject.getString("name");
String jsonString = "{ 'age':10,'groupTag':'A','name':'张三','score':23 }";
JSONObject jsonObject = JSONObject.parseObject(jsonString);
Hero hero = jsonObject.to(Hero.class);
String jsonString = "{" +
"'张三': { 'age':10,'groupTag':'A','name':'张三','score':23 }," +
"'李四': { 'age':11,'groupTag':'B','name':'李四','score':28 }" +
"}";
JSONObject jsonObject = JSONObject.parseObject(jsonString);
Object obj = jsonObject.get("张三");
System.out.println(obj); // {"age":10,"groupTag":"A","name":"张三","score":23}
Hero hero = jsonObject.getObject("李四", Hero.class);
System.out.println(hero); // Hero(name=李四, age=11, score=28, groupTag=B)
String jsonString = "[" +
"{ 'age':10,'groupTag':'A','name':'张三','score':23 }," +
"{ 'age':11,'groupTag':'B','name':'李四','score':28 }" +
"]";
JSONArray jsonArray = JSONArray.parseArray(jsonString);
Object o = jsonArray.get(0);
System.out.println(o); // {"age":10,"groupTag":"A","name":"张三","score":23}
Hero hero = jsonArray.getObject(0, Hero.class);
System.out.println(hero); // Hero(name=张三, age=10, score=23, groupTag=A)
String jsonString = "[" +
"{ 'age':10,'groupTag':'A','name':'张三','score':23 }," +
"{ 'age':11,'groupTag':'B','name':'李四','score':28 }" +
"]";
JSONArray jsonArray = JSON.parseArray(jsonString);
List<Hero> list = jsonArray.toJavaList(Hero.class);
System.out.println(list);
// [Hero(name=张三, age=10, score=23, groupTag=A), Hero(name=李四, age=11, score=28, groupTag=B)]
String jsonString = "[" +
"{ 'age':10,'groupTag':'A','name':'张三','score':23 }," +
"{ 'age':11,'groupTag':'B','name':'李四','score':28 }" +
"]";
List<Hero> list = JSON.parseArray(jsonString, Hero.class);
System.out.println(list);
// [Hero(name=张三, age=10, score=23, groupTag=A), Hero(name=李四, age=11, score=28, groupTag=B)]
String jsonString = "{" +
"'张三': { 'age':10,'groupTag':'A','name':'张三','score':23 }," +
"'李四': { 'age':11,'groupTag':'B','name':'李四','score':28 }" +
"}";
TypeReference<HashMap<String, Hero>> tr = new TypeReference<HashMap<String, Hero>>() {};
HashMap<String, Hero> map = JSONObject.parseObject(jsonString, tr);
System.out.println(map);
// {李四=Hero(name=李四, age=11, score=28, groupTag=B), 张三=Hero(name=张三, age=10, score=23, groupTag=A)}
给定 JSON字符串
和路径
解析出对象。
JSONPath.eval(JavaBean, "JSONPathStr")
List<Hero> list = Hero.getList();
String jsonString = JSONObject.toJSONString(list);
Hero hero = (Hero)JSONPath.eval(jsonString, "$[0]");
System.out.println(hero); // Hero(name=张三, age=10, score=23, groupTag=A)
JSONObject jsonObject = (JSONObject)JSONPath.eval(jsonString, "$[0]");
Hero hero = jsonObject.to(Hero.class);
System.out.println(hero); // Hero(name=张三, age=10, score=23, groupTag=A)
String name = (String)JSONPath.eval(jsonString, "$[0].name");
System.out.println(name); // 张三
列表也是支持的
String jsonString = "[" +
"{ 'age':10,'groupTag':'A','name':'张三','score':23 }," +
"{ 'age':11,'groupTag':'B','name':'李四','score':28 }" +
"]";
JSONArray array = (JSONArray)JSONPath.eval(jsonString, "$");
List<Hero> list = array.toJavaList(Hero.class);
System.out.println(list);
用于多次读取的情况
JSONReader.of("jsonString");
JSONReader parser = JSONReader.of("[ {\"age\":10,\"groupTag\":\"A\",\"name\":\"张三\",\"score\":23} ]");
JSONPath path = JSONPath.of("$[0].name"); // 缓存起来重复使用能提升性能
String name = (String)path.extract(parser);
System.out.println(name); // 张三