Alibaba Fastjson的基本使用

目录

1. 基本说明

2. 引入依赖

3. 其他类型转Json对象

3.1 json字符串转JsonOject

3.2 json数组字符串转JsonArray

3.3 JavaBean 转 JsonObject

3.4 JavaBean List转JsonArray

4. 其他类型转Json字符串

4.1 将Map转Json字符串

4.2 将Java Bean 转Json字符串 

4.3 将JavaBean List转Json字符串

1. 基本说明

FastJson 与 Google 的 Gson 都是解析 Json 的强者

github地址: GitHub - alibaba/fastjson: FASTJSON 2.0.x has been released, faster and more secure, recommend you upgrade.

2. 引入依赖

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.49</version>
</dependency>

3. 其他类型转Json对象

3.1 json字符串转JsonOject

    /**
     * json字符串转JsonOject
     */
    public static void test1() {
        String jsonStr = "{\"name\":\"sanqian\",\"age\":10,\"address\": \"shen zhen\"}";
        JSONObject jsonObject = JSONObject.parseObject(jsonStr);
        System.out.println(jsonObject.getString("name"));//输出one
        System.out.println(jsonObject.getInteger("age"));//输出110
        System.out.println(jsonObject.getString("address"));//输出null
    }

3.2 json数组字符串转JsonArray

    /**
     * json数组字符串转JsonArray
     */
    public static void test2(){
        String jsonArrStr = "[{\"name\":\"张三\",\"age\":25},{\"name\":\"李四\",\"age\":28}]";
        JSONArray jsonArray = JSONObject.parseArray(jsonArrStr);
        for(Object object: jsonArray){
            JSONObject jsonObject = (JSONObject) object;
            System.out.println(jsonObject.get("name"));
            System.out.println(jsonObject.get("age"));
            System.out.println("--------------------");
        }
    }

3.3 JavaBean 转 JsonObject

    /**
     * JavaBean 转 JsonObject
     */
    public static void test3(){
        Person person1 = new Person();
        person1.setName("张三");
        person1.setAge(28);
        person1.setBirthday(new Date());
        // 方式一:
        JSONObject jsonObject = (JSONObject) JSONObject.toJSON(person1);
        System.out.println(jsonObject);
        System.out.println(jsonObject.get("name"));
        //方式二
        String jsonString = JSONObject.toJSONString(person1);
        JSONObject jsonObject1 = JSONObject.parseObject(jsonString);
        System.out.println(jsonObject1);
    }

3.4 JavaBean List转JsonArray

    /**
     * JavaBean List转JsonArray
     */
    public static void test4(){
        Person person1 = new Person();
        person1.setName("张三");
        person1.setAge(28);
        person1.setBirthday(new Date());

        Person person2 = new Person();
        person2.setName("李四");
        person2.setAge(25);
        person2.setBirthday(new Date());

        List<Person> persons = new ArrayList<Person>();
        persons.add(person1);
        persons.add(person2);

        /**方式1*/
        String jsonArrStr = JSONArray.toJSONString(persons);
        JSONArray jsonArray = JSONArray.parseArray(jsonArrStr);
        JSONObject jsonObject1 = (JSONObject)jsonArray.get(0);
        System.out.println(jsonObject1.get("name"));//输出:张三

        /**方式2*/
        JSONArray jsonArray1 = (JSONArray)JSONArray.toJSON(persons);
        JSONObject jsonObject2 = (JSONObject)jsonArray1.get(1);
        System.out.println(jsonObject2.get("name"));//输出:李四
    }

4. 其他类型转Json字符串

4.1 将Map转Json字符串

    /**
     * 将Map转Json字符串
     */
    public void test1() {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("key1", "One");
        map.put("key2", "Two");
        String mapJson = JSON.toJSONString(map);
        System.out.println(mapJson);//输出:{"key1":"One","key2":"Two"}
    }

4.2 将Java Bean 转Json字符串 

    /**
     * 将Java Bean 转Json字符串
     */
    public static void test2() {
        Person person1 = new Person();
        person1.setName("张三");
        person1.setAge(26);
        person1.setBirthday(new Date());

        /**两种方式都行
         * 因为JSONObject继承了JSON*/
        String object = JSONObject.toJSONString(person1);
        /*String object = JSON.toJSONString(person1);*/
        System.out.println(object);
    }

4.3 将JavaBean List转Json字符串

    /**
     * 将Java Bean List转Json字符串
     */
    public static void test3() {
        Person person1 = new Person();
        person1.setName("张三");
        person1.setAge(28);
        person1.setBirthday(new Date());

        Person person2 = new Person();
        person2.setName("李四");
        person2.setAge(25);
        person2.setBirthday(new Date());

        List<Person> persons = new ArrayList<Person>();
        persons.add(person1);
        persons.add(person2);

        String object = JSON.toJSONString(persons);
        System.out.println(object);
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值