首页 > 其他分享 >fastjson的使用

fastjson的使用

时间:2024-05-14 14:54:56浏览次数:13  
标签:fastjson Java Person JSON 使用 new public

  1. Json是一种轻量级的数据交换格式,应该在一个程序员的开发生涯中是常接触的。简洁和清晰的层次结构使得 JSON 成为理想的数据交换语言。 易于人阅读和编写,同时也易于机器解析和生成,并有效地提升网络传输效率。

  2. Fastjson是一个Java库,可用于将Java对象转换为其JSON表示形式,也可以用于将JSON字符串转换为等效的Java对象。Fastjson可以与任意Java对象一起使用。

  3. fastjson的优点

    • 速度快
      • fastjson相对其他JSON库的特点是快,从2011年fastjson发布1.1.x版本之后,其性能从未被其他Java实现的JSON库超越。
    • 使用广泛
      • fastjson在阿里巴巴大规模使用,在数万台服务器上部署,fastjson在业界被广泛接受。在2012年被开源中国评选为最受欢迎的国产开源软件之一。
    • 测试完备
      • fastjson有非常多的testcase,在1.2.11版本中,testcase超过3321个。每次发布都会进行回归测试,保证质量稳定。
    • 使用简单
      • fastjson的 API 十分简洁。
    • 功能完备
      • 支持泛型,支持流处理超大文本,支持枚举,支持序列化和反序列化扩展。
  4. 导入依赖

    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>xxx</version>
    </dependency>
    
  5. fastjson的使用

    • 实体类
    public class Person {
     
    @JSONField(name = "AGE")
    private int age;
    
    @JSONField(name = "FULL NAME")
    private String fullName;
    
    @JSONField(name = "DATE OF BIRTH")
    private Date dateOfBirth;
    
    public Person(int age, String fullName, Date dateOfBirth) {
        super();
        this.age = age;
        this.fullName= fullName;
        this.dateOfBirth = dateOfBirth;
    }
    
    // 标准 getters & setters
    }
    
    • 使用 JSON.toJSONString() 将 Java 对象转换换为 JSON 对象:
    private List<Person> listOfPersons = new ArrayList<Person>();
     
    @Before
    public void setUp() {
        listOfPersons.add(new Person(15, "John Doe", new Date()));
        listOfPersons.add(new Person(20, "Janette Doe", new Date()));
    }
     
    @Test
    public void whenJavaList_thanConvertToJsonCorrect() {
        String jsonOutput= JSON.toJSONString(listOfPersons);
    }
    
    • 使用 JSON.parseObject() 将 JSON 字符串转换为 Java 对象
    @Test
    public void whenJson_thanConvertToObjectCorrect() {
        Person person = new Person(20, "John", "Doe", new Date());
        String jsonObject = JSON.toJSONString(person);
        Person newPerson = JSON.parseObject(jsonObject, Person.class);
         
        assertEquals(newPerson.getAge(), 0); // 如果我们设置系列化为 false
        assertEquals(newPerson.getFullName(), listOfPersons.get(0).getFullName());
    }
    

标签:fastjson,Java,Person,JSON,使用,new,public
From: https://www.cnblogs.com/hytip/p/18191270

相关文章

  • 使用Redis执行多个命令
    单个命令执行packagemainimport("context""fmt""github.com/go-redis/redis/v8""time")funcmain(){//创建Redis客户端rdb:=redis.NewClient(&redis.Options{Addr:"localhos......
  • Linux 使用 curl 发送请求
    前言请各大网友尊重本人原创知识分享,谨记本人博客:南国以南i、提示:以下是本篇文章正文内容,下面案例可供参考#get请求json入参curlhttp://xxx.xxx:60512/scrm/chat/msg/selectFullChatListForDay-d'{"key":"215EACF20778586C788FFD9187AB0F72","qrDay":"2023-04-25"}�......
  • Jmeter - BeanSell 后置处理器 结合 HttpClient 使用
    背景:在后置处理器中发送POST请求,请求体为JSON数据疑问:1.如果获取Cookie?2.HttpClient怎么发送POST?3.HttpClient怎么添加Cookie?解决:1.如果获取Cookie?importorg.apache.jmeter.protocol.http.control.CookieManager;importorg.apache.jmeter.protocol.http.control.Coo......
  • 使用快捷键的方式把多个关键字文本快速替换(快速替换AE脚本代码)
    首先,需要用到的这个工具:度娘网盘提取码:qwu2蓝奏云提取码:2r1z 这里做AE(AdobeAfterEffact)里的脚本规则,把英文替换成中文,如下swap=thisComp.layer(“Segmentsettings”).effect("%")(“Checkbox”);if(swap==true){s=thisComp.layer(“Segmentsettings”).effect(“S......
  • nacos接口使用
    1.nacos官方文档https://nacos.io/zh-cn/docs/open-api.html 2.参数获取 3.nacos例子(其余例子可参考文档)#调用api发布配置curl-XPOST-F"[email protected]"-F"content=$(<cv-platfom-common.yaml)"-F"type=yaml""http://172.16......
  • 【词典】安卓系统使用 深蓝词典(BlueDict) & IOS系统使用 欧路词典(Eudic)
      之前在WindowsMobile中有一款词典软件——MDict(开发者为RaymanZhang,官方网站地址:http://www.octopus-studio.com/),可以支持超多的词典扩展,比如维基百科全书、汉语词典、唐诗宋词词典等。后来安卓版本的同样功能的软件面试,让我更加坚定转投安卓手机,这就是Bluedict,全面兼容M......
  • VUE3.0 中如何使用SVG图标
    1.文件下新建SvgIcon文件夹以及子文件index.js,index.vue,svg文件夹(用于存放svg图片) 2.编写index.vue组件<template><svg:class="svgClass"aria-hidden="true"><use:xlink:href="iconName"/></svg></template><......
  • 程序员的AI编程小助手,CodeGeeX在vscode,vs2022,IDEA2023使用体验总结
    程序员的AI编程小助手,CodeGeeX使用体验总结一、1.CodeGeeX是什么?能做什么?CodeGeeX是一个智能编程软件工具,目前CodeGeeX支持多种主流IDE,如VSCode、visualstudio2022,IntelliJIDEA、PyCharm、Vim等,同时,支持Python、Java、C++/C、JavaScript、Go等多种语言。CodeGeeX如何......
  • Mybatis使用cache-ref需注意的问题
    https://blog.csdn.net/Ewan_/article/details/1298659741.在MapperXML中使用<cache-ref>标签UserCacheMapperXML需要有<cache>标签,如果没有则会爆Exceptioninthread"main"org.apache.ibatis.builder.IncompleteElementException:Nocachefornamespace'......
  • 使用Docker容器运行jupyter
    jupyter运行ccdjupyter-c-kerneldockerrun-v$(pwd):/jupyter/jupyter_c_kernel/-v/home/jovyan/work:/home/jovyan/work-p8888:8888brendanrius/jupyter-c-kernel#注意给/home/jovyan/work权限MinimalCkernelforJupyterUsewithDocker(recommended)docker......