fastjson2基本使用

据说fastjson2在性能基础上有很大提升,具体哪些内容提升可以看看这个地址https://github.com/alibaba/fastjson2/wiki/fastjson_benchmark

开始上手:

1.添加maven依赖

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

2.常用的类跟方法(基本跟fastJson一样)

3.读取JSON对象

package com.yicall.screen.controller;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;

/**
 * @Author 谢 洁
 * @Date 2022/5/3 20:43
 * @Version 1.0
 */
public class FastJson2Test {

    public static void main(String[] args){

        String str = "{\"id\":100}";
        JSONObject jsonObject = JSON.parseObject(str);
        int id = jsonObject.getIntValue("id");
        String str1 = "[\"id\", 100]";
        JSONArray jsonArray = JSON.parseArray(str);
        String name = jsonArray.getString(0);
        int id1 = jsonArray.getIntValue(1);

        System.out.println("id--"+id+"--id1--"+id1+"--str--"+str+"--str1--"+str1);
    }
}

4.使用JSONPath

package com.yicall.screen.controller;

import com.alibaba.fastjson2.*;

import java.util.List;

/**
 * @Author 谢 洁
 * @Date 2022/5/3 21:05
 * @Version 1.0
 */
public class FastJson2Test {

    public static void main(String[] args){

        String json = "{\"store\":{\"book\":[{\"title\":\"第一本书\",\"price\":10},{\"title\":\"第二本书\",\"price\":12},{\"title\":\"第三本书\",\"isbn\":\"553\",\"price\":8},{\"title\":\"第四本书\",\"isbn\":\"395\",\"price\":22}],\"bicycle\":{\"color\":\"red\",\"price\":19}}}";

        // 获取json中store下book下的所有title值
        List<Object> titles = (List<Object>) JSONPath.eval(json, "$.store.book.title");
        System.out.println("$.store.book.title = " + titles);
//结果:$.store.book.title = ["第一本书","第二本书","第三本书","第四本书"]

    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值