测试-FastJSON和Jackson-JSON库

文章目录

FastJSON

直接读代码:

java 复制代码
@Log4j2
public class FastJsonTest {

    @Test
    public void test1() {
        // JSON转对象-parseObject
        String jsonString = "{\"name\":\"张三\",\"age\":18}";
        Student student = JSON.parseObject(jsonString, Student.class);
        log.info(JSON.toJSONString(student));
    }

    @Test
    public void test2() {
        // json转list-parseArray
        String jsonString = "[{\"name\":\"张三\",\"age\":18},{\"name\":\"李四\",\"age\":19}]";
        List<Student> students = JSON.parseArray(jsonString, Student.class);
        for (Student student : students) {
            log.info(JSON.toJSONString(student));
        }
    }

    @Test
    public void test3() {
        // 对象转JSON
        Student student = new Student();
        student.setName("Gjkt");
        student.setAge(3);
        log.info(JSON.toJSONString(student));
    }

    @Test
    public void test4() {
        // list转JSON
        List<Student> list = new ArrayList<>();
        Student student1 = new Student();
        Student student2 = new Student();
        student1.setName("张三");
        student2.setName("李四");
        student1.setAge(3);
        student2.setAge(6);
        Collections.addAll(list, student1, student2);
        log.info(JSON.toJSONString(list));
    }

    // -----------------JSONObject-来自fastJson2----------------------
    @Test
    public void test5() {
        // JSON转JSONObject
        String jsonString = "{\"name\":\"张三\",\"age\":18}";
        JSONObject jsonObject = JSON.parseObject(jsonString);
        log.info(JSON.toJSONString(jsonObject));
    }

    @Test
    public void test6() {
        // JSONObject转JSON
        String jsonString = "{\"name\":\"张三\",\"age\":18}";
        JSONObject jsonObject = JSON.parseObject(jsonString);
        String name = jsonObject.getString("name");
        log.info(name);
    }

    @Test
    public void test7() {
        // JSONObject转List(JSONObject->String->List)
        String jsonString = "[{\"name\":\"张三\",\"age\":18},{\"name\":\"李四\",\"age\":19}]";
//        log.info(t);
        List<Student> list = JSONArray.parseArray(jsonString, Student.class);
        for (Student student : list) {
            log.info(student.toString());
        }
        log.info(JSON.toJSONString(list));
    }

    @Test
    public void test8() {
        // Map转JSONObject
        Map<String, Student> map = new HashMap<>();
        //
        Student student1 = new Student();
        Student student2 = new Student();
        student1.setName("张三");
        student2.setName("李四");
        student1.setAge(3);
        student2.setAge(6);
        //
        map.put("one", student1);
        map.put("two", student2);
        JSONObject json = (JSONObject) JSON.toJSON(map);
        log.info(json);
    }

    @Test
    public void test9() {
    // fastJSON不会报错,会序列化为空JSON对象
        String jsonString = "{\"name-cs\":\"张三\",\"age-cs\":18}";
        JSONObject jsonObject = JSON.parseObject(jsonString);
        Student student = JSON.to(Student.class, jsonObject);
        log.info(student);
    }

}

Jackson

java 复制代码
@Log4j2
public class JacksonTest {
    protected final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Test
    public void test1() throws JsonProcessingException {
        // JacksonTest-json键值对,不匹配或多于实体类字段
//        String jsonString = "{\"name\":\"张三\",\"age\":18}";
        String jsonString = "{\"name\":\"张三\",\"age\":18,\"cs\":\"雪豹闭嘴\"}";
        log.info(jsonString);
        ObjectMapper objectMapper = new ObjectMapper();
        Student student = objectMapper.readValue(jsonString, Student.class);
        log.info(student);
    }
}

注意

Jackson和Fastjson是两个常用的Java库,都用于处理JSON数据。

都提供了将Java对象序列化为JSON格式以及将JSON数据反序列化为Java对象的功能。

Jackson和Fastjson在序列化机制上存在一些差异。

  • Jackson的序列化机制依赖于JavaBean的规范,要求对象有对应属性的get方法。如果一个属性没有对应的get方法,Jackson在序列化时会抛出异常

com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException:

解决方案:实体类上添加@JsonIgnoreProperties(ignoreUnknown = true) 注解

表示:

  1. 当将一个对象序列化为 JSON 时,如果该对象有任何未知的属性 (即该对象的类中没有对应的字段或getter/setter方法),这些属性将被忽略,不会被序列化到 JSON 中
  2. 当从 JSON 反序列化一个对象 时,如果 JSON 中有任何未知的属性,Jackson 将忽略它们,而不会抛出异常或错误。这意味着你可以向一个对象添加额外的 JSON 属性,而不会引发异常。
  • Fastjson则采用基于JavaBean的序列化和反序列化机制,在序列化时通过反射获取对象 的属性信息,再通过get方法获取属性值,最终将其转换为JSON字符串。如果一个属性没有对应的get方法,Fastjson无法获取到其值,导致序列化结果为空的JSON对象。
相关推荐
非凡ghost12 小时前
CoolUtils PDF Combine(PDF合并工具)
windows·学习·pdf·软件需求
就叫飞六吧12 小时前
JSONPath“隔空取物”思想,直击JSON深处的目标字段
服务器·windows·json
love530love12 小时前
【笔记】ComfyUI “OSError: [WinError 38] 已到文件结尾” 报错解决方案
人工智能·windows·python·aigc·comfyui·winerror 38
卓码软件测评15 小时前
第三方软件测评机构:【Gatling构建JSON请求体StringBody、ElFileBody和Pebble模板的使用】
测试工具·性能优化·json·测试用例
爱敲点代码的小哥15 小时前
json序列化和反序列化 和 数组转成json格式
java·前端·json
Shi_haoliu16 小时前
inno setup6.6.1实例,制作安装包,创建共享文件夹,写入注册表(提供给excel加载项,此文章解释iss文件)
前端·vue.js·windows·excel
nnsix16 小时前
文件系统、分配单元大小 什么意思
windows
Boxsc_midnight17 小时前
【数字人学习之语音合成】Fun-CosyVoice3-0.5B-2512的windows系统中本地部署的方法
windows·学习·cosyvoice3
Zfox_18 小时前
无缝穿越系统边界:节点小宝4.0如何让我的Mac/iOS像访问本地盘一样操控Windows
windows·macos·ios·节点小宝
嵌入式学习和实践18 小时前
Linux/Windows 系统架构查看、安装包选择指南(嵌入式开发场景适配)
linux·windows·系统架构