当前位置: 首页 > 工具软件 > fastJSON 2 > 使用案例 >

FastJSON2 学习笔记

丁星火
2023-12-01


FASTJSON2是FASTJSON项目的重要升级,目标是为下一个十年提供一个高性能的JSON库。官方如是说。

pom.xml

查看新版本: maven.org

        <dependency>
            <groupId>com.alibaba.fastjson2</groupId>
            <artifactId>fastjson2</artifactId>
            <version>2.0.12</version>
        </dependency>

非完美兼容 fastjson1

如果原来使用fastjson 1.2.x版本,可以使用兼容包,兼容包不能保证100%兼容,请仔细测试验证,发现问题请及时反馈。

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>2.0.14</version>
</dependency>

测试数据

JavaBean

另一篇文章里有,直接取来用。

Json String

[
    {"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"}
]

序列化

toJSONString

Hero hero = Hero.getList().get(0);
String jsonString = JSONObject.toJSONString(hero);
{"age":10,"groupTag":"A","name":"张三","score":23}

Features

更多详情,见参考资料:通过Features配置序列化和反序列化的行为

1. 格式化输出

Hero hero = Hero.getList().get(0);
String jsonString = JSON.toJSONString(hero, JSONWriter.Feature.PrettyFormat);
{
	"age":10,
	"groupTag":"A",
	"name":"张三",
	"score":23
}

2. 序列化时输出类型信息

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
}

3. 使用单引号

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 }

4. 序列话时包含为 null 的字段

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 }

反序列化

JSONObject

jsonString 转 JavaBean

String jsonString = "{ 'age':10,'groupTag':'A','name':'张三','score':23 }";
Hero hero = JSONObject.parseObject(jsonString, Hero.class);

jsonString 转 JSONObject

String jsonString = "{ 'age':10,'groupTag':'A','name':'张三','score':23 }";
JSONObject jsonObject = JSONObject.parseObject(jsonString);

int id = jsonObject.getIntValue("age");
String name = jsonObject.getString("name");

JSONObject 转 JavaBean

String jsonString = "{ 'age':10,'groupTag':'A','name':'张三','score':23 }";
JSONObject jsonObject = JSONObject.parseObject(jsonString);

Hero hero = jsonObject.to(Hero.class);

JSONObject 获取 JavaBean

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)

JSONArray

jsonString 转 JSONArray

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)

JSONArray 转 List

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)]

jsonString 转 List

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)]

jsonString 转 Map

 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)}

JSONPath

eval

给定 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);

of

用于多次读取的情况

  • 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); // 张三

参考资料

Fastjson_学习笔记

GitHub:alibaba/fastjson2

Gitee:wenshao:fastjson2

Gitee:astjson2文档

 类似资料: