fastjson2常用方法

FASTJSON 2.0是FASTJSON项目的重要升级,目标是为下一个十年提供一个高性能的JSON库,同一套API支持JSON/JSONB两种协议,JSONPath是一等公民,支持全量解析和部分解析,支持Java服务端、客户端Android、大数据场景。

  • FASJTONS2代码 https://github.com/alibaba/fastjson2/releases/tag/2.0.1

  • JSONB格式文档 https://github.com/alibaba/fastjson2/wiki/jsonb_format_cn

  • FASTJSON 2性能有了很大提升,具体性能数据看这里 https://github.com/alibaba/fastjson2/wiki/fastjson_benchmark

maven依赖:

  1.  com.alibaba.fastjson2

  2.  fastjson2

  3.  2.0.1

public class FastJsonTest {
    public static void main(String[] args) {

        /**
         * Json类
         * */
        //将字符串解析成JsonObject对象
        //String s="{\"name\":'张三',\"age\":20,\"address\":'太原'}";
        String s = "{name:'张三',age:20,address:'太原'}";
        JSONObject object = JSON.parseObject(s);

        Integer age = object.getInteger("age"); //获取age的value值 20
        Object o = object.get("name"); //根据key获取对应的值 张三,返回值integer
        int i = object.getIntValue("age"); //根据key获取age的值 20,返回值int
        Object name = object.getObject("name", String.class); //根据指定key和value类型找到对应值 张三
//        System.out.println(name);
//        System.out.println(i);

        //将JsonObject对象转换为Java对象
        Student student3 = JSON.toJavaObject(object, Student.class);
        System.out.println(student3); //Student(name=张三, age=20, address=太原)

        String s1 = "[{'name':'张三','age':20,'address':'太原'},{'name':'李四','age':40,'address':'新疆'}]";
        //将字符串解析成数组
        List list = JSON.parseArray(s1, Student.class);
        System.out.println(list); //[Student(name=张三, age=20, address=太原), Student(name=李四, age=40, address=新疆)]

        //将字符串解析成JsonArray对象
        String ss = "[{name:'张三',age:20,address:'太原'}]";
        JSONArray array = JSON.parseArray(ss);

        System.out.println(array.getString(0)); //[{"name":"张三","age":20,"address":"太原"},{"name":"李四","age":40,"address":"新疆"}]

        //将字符串解析成Java对象
        String s2 = "{\"name\":'张三',\"age\":20,\"address\":'太原'}";
        Student student = JSON.parseObject(s2, Student.class);
        System.out.println(student);

        //将Java对象转换成字符串
        String s3 = JSON.toJSONString(student);
        System.out.println(s3); //{"address":"太原","age":20,"name":"张三"}

        //将Java对象输出成byte[]
        byte[] bytes = JSON.toJSONBytes(student);
        System.out.println(Arrays.toString(bytes)); //[123, 34, 97, 100, 100, 114, 101, 115, 115](数组过长取了一部分)

        //将byte[]装换为Java对象
        Student student1 = JSON.parseObject(bytes, Student.class);
        System.out.println(student1); //Student(name=张三, age=20, address=太原)


        /**
         * JsonB
         * */
        //将Java对象解析成jsonb格式的byte []
        byte[] bytes1 = JSONB.toBytes(student);
        System.out.println(Arrays.toString(bytes1)); //[-90, 80, 97, 100, 100, 114, 101, 115, 115, 122, 6, -27, -92](数组元素只取了一部分)

        //将jsonb格式的byte [] 解析成Java对象
        Student student2 = JSONB.parseObject(bytes1, Student.class);
        System.out.println(student2); //Student(name=张三, age=20, address=太原)

        /**
         * JsonPath
         * */
        //构建JSONPath
        //使用JSONPath读取部分数据
        String sss = "{\"name\":'小明'}";
        JSONPath of = JSONPath.of("$name");
        System.out.println(of); //$name
        JSONReader reader = JSONReader.of(sss);
        Object o1 = of.extract(reader);
        System.out.println(o1); //小明


    }
}
 
  

注意:上面String字符串格式是统一的,不能随遍写,尤其是引号 

 详情可参考:Fastjson 2 来了,性能继续提升,还能再战十年_程序猿DD_的博客-CSDN博客

你可能感兴趣的:(Java,json)