Fastjson2
是 Fastjson
的升级版本,提供了更好的性能和扩展性,同时也在 API 和功能上做了很多改进。使用 Fastjson2
解析 JSON 数据非常简单,支持多种方式来解析 JSON 字符串、嵌套 JSON 对象和数组、以及转换成 Java 对象。下面详细介绍 Fastjson2
的常见 JSON 解析用法。
1. 引入 Fastjson2 依赖
在使用 Fastjson2
之前,确保项目中包含相应的依赖。
Maven 依赖
1 | com.alibaba.fastjson2fastjson22.0.31 |
Gradle 依赖
1 | implementation 'com.alibaba.fastjson2:fastjson2:2.0.31' |
2. JSON 解析
2.1 解析 JSON 字符串为 Java 对象
Fastjson2
使用 JSON.parseObject()
方法将 JSON 字符串转换为 Java 对象。
示例:将 JSON 字符串解析为 Java 对象
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | import com.alibaba.fastjson2.JSON; public class Fastjson2Example { public static void main(String[] args) { String jsonString = "{" name ":" John "," age ":25}" ; // 解析 JSON 字符串为 Java 对象 Person person = JSON.parseObject(jsonString, Person. class ); System.out.println(person.getName()); // 输出: John System.out.println(person.getAge()); // 输出: 25 } } class Person { private String name; private int age; // Getters and Setters public String getName() { return name; } public void setName(String name) { this .name = name; } public int getAge() { return age; } public void setAge( int age) { this .age = age; } } |
2.2 解析嵌套的 JSON 对象
Fastjson2
可以直接解析嵌套的 JSON 对象。你可以通过 getJSONObject()
获取嵌套的 JSONObject
,然后再继续解析它。
示例:解析嵌套的 JSON 对象
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | import com.alibaba.fastjson2.JSON; import com.alibaba.fastjson2.JSONObject; public class Fastjson2Example { public static void main(String[] args) { String jsonString = "{" name ":" John "," address ":{" city ":" Anytown "," street ":" 123 Main St "}}" ; // 解析 JSON 字符串为 JSONObject JSONObject jsonObject = JSON.parseObject(jsonString); // 获取嵌套的 JSON 对象 (address) JSONObject address = jsonObject.getJSONObject( "address" ); String city = address.getString( "city" ); String street = address.getString( "street" ); System.out.println( "City: " + city); // 输出: Anytown System.out.println( "Street: " + street); // 输出: 123 Main St } } |
2.3 解析 JSON 数组
Fastjson2
也可以直接将 JSON 数组字符串解析为 JSONArray
。你可以通过 parseArray()
或 parseObject()
方法来处理数组。
示例:解析 JSON 数组
1 2 3 4 5 6 7 8 9 | import com.alibaba.fastjson2.JSON; import com.alibaba.fastjson2.JSONArray; public class Fastjson2Example { public static void main(String[] args) { String jsonArrayString = "[{" name ":" John "," age ":25}, {" name ":" Jane "," age ":28}]" ; // 将 JSON 数组解析为 List JSONArray jsonArray = JSON.parseArray(jsonArrayString); // 遍历 JSON 数组并解析每个对象 for ( int i = 0 ; i |
2.4 解析 JSON 字符串为 Map
如果你不需要将 JSON 解析为特定的 Java 对象,可以直接解析为 Map
或 List
。
示例:将 JSON 解析为 Map
1 2 3 4 5 6 7 8 9 10 11 | import com.alibaba.fastjson2.JSON; import java.util.Map; public class Fastjson2Example { public static void main(String[] args) { String jsonString = "{" name ":" John "," age ":25}" ; // 将 JSON 解析为 Map Map map = JSON.parseObject(jsonString, Map. class ); // 输出 Map 内容 System.out.println(map); // 输出: {name=John, age=25} } } |
2.5 解析 JSON 数据时的类型转换
Fastjson2
支持复杂的类型转换。你可以将 JSON 解析为 List
、Map
、以及任意的 Java 类型。
示例:JSON 转换为 List
和 Map
1 2 3 4 5 6 7 8 9 10 11 | import com.alibaba.fastjson2.JSON; import java.util.List; import java.util.Map; public class Fastjson2Example { public static void main(String[] args) { String jsonArrayString = "[{" name ":" John "," age ":25}, {" name ":" Jane "," age ":28}]" ; // 将 JSON 数组字符串解析为 List List<map>> personList = JSON.parseArray(jsonArrayString, Map. class ); System.out.println(personList); // 输出: [{name=John, age=25}, {name=Jane, age=28}] } }</map> |
3. 高级功能
3.1 使用 JSONPath 从 JSON 中提取数据
Fastjson2
提供了 JSONPath
功能,支持复杂的查询操作,类似于 XPath 的功能。可以使用 JSONPath.eval()
来提取 JSON 数据。
示例:使用 JSONPath
提取 JSON 数据
1 2 3 4 5 6 7 8 9 10 | import com.alibaba.fastjson2.JSON; import com.alibaba.fastjson2.JSONPath; public class Fastjson2Example { public static void main(String[] args) { String jsonString = "{" name ":" John ", " address ":{" city ":" Anytown "," street ":" 123 Main St "}}" ; // 使用 JSONPath 提取城市信息 Object city = JSONPath.eval(JSON.parse(jsonString), "$.address.city" ); System.out.println( "City: " + city); // 输出: Anytown } } |
3.2 处理自定义日期格式
你可以为 Fastjson2
设置自定义的日期格式,通过 SerializeConfig
配置。
示例:自定义日期格式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | import com.alibaba.fastjson2.JSON; import com.alibaba.fastjson2.serializer.SerializeConfig; import java.text.SimpleDateFormat; import java.util.Date; public class Fastjson2Example { public static void main(String[] args) { Date date = new Date(); // 设置自定义日期格式 SerializeConfig config = new SerializeConfig(); config.put(Date. class , new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" )); // 将日期对象转换为 JSON 字符串 String jsonString = JSON.toJSONString(date, config); System.out.println(jsonString); // 输出: 2025-01-13 14:30:00 } } |
3.3 反序列化时处理空字段
Fastjson2
在反序列化 JSON 时,如果某些字段为空或者不存在,它会自动跳过该字段并不会抛出异常。你可以使用 @JSONField
注解来控制字段的序列化和反序列化行为。
示例:处理空字段
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | import com.alibaba.fastjson2.JSON; import com.alibaba.fastjson2.annotation.JSONField; public class Fastjson2Example { public static void main(String[] args) { String jsonString = "{" name ":" John "," age ":null}" ; // 解析 JSON 字符串时忽略空字段 Person person = JSON.parseObject(jsonString, Person. class ); System.out.println(person.getName()); // 输出: John System.out.println(person.getAge()); // 输出: 0 (默认值) } } class Person { private String name; @JSONField (serialize = false ) // 不序列化 private int age; // Getters and Setters public String getName() { return name; } public void setName(String name) { this .name = name; } public int getAge() { return age; } public void setAge( int age) { this .age = age; } } |
4. 总结
Fastjson2
提供了强大且简洁的 JSON 解析功能,包括:
- 轻松将 JSON 字符串解析为 Java 对象或
Map
。 - 支持嵌套的 JSON 对象、数组和复杂类型的解析。
- 支持通过
JSONPath
从 JSON 数据中提取数据。 - 自定义日期格式和序列化配置。
- 处理反序列化时的空字段、字段过滤等。
这些特性使得 Fastjson2
成为一个高效且功能丰富的 JSON 解析工具,适用于各种不同的 Java 应用场景。
到此这篇关于java fastjson2 解析JSON用法解析的文章就介绍到这了,更多相关java fastjson2 解析JSON内容请搜索IT俱乐部以前的文章或继续浏览下面的相关文章希望大家以后多多支持IT俱乐部!