阿里巴巴 - fastjson API

Fastjson是阿里巴巴开发的Java工具包,用于处理JSON数据,支持序列化和反序列化。主要API包括JSON对象和数组的操作,如parseObject、parseArray、toJSONString等,便于快速在JSON文本和Java对象间转换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式。 易于人阅读和编写。同时也易于机器解析和生成。JSON采用完全独立于语言的文本格式
  2. Fastjson是阿里巴巴内部开发的用于java后台处理json格式数据的一个工具包,包括“序列化”和“反序列化”两部分
  3. 下面是FastJson的简介:常用的方法
    Fastjson API入口类是com.alibaba.fastjson.JSON,常用的序列化操作都可以在JSON类上的静态方法直接完成。
    public static final Object parse(String text);//把JSON文本parse为一个Object
    public static final JSONObject parseObject(String text);//把JSON文本parse成JSONObject
    public static final <T> T parseObject(String text, Class<T> clazz);//把JSON文本parse为JavaBean
    public static final JSONArray parseArray(String text);//把JSON文本parse成JSONArray
    public static final <T> List<T> parseArray(String text, Class<T> clazz);//把JSON文本parse成JavaBean集合
    public static final String toJSONString(Object object);// 将JavaBean序列化为JSON文本
    public static final String toJSONString(Object object, boolean prettyFormat);//将JavaBean序列化为带格式的JSON文本
    public static final Object toJSON(Object javaObject);//将JavaBean转换为JSONObject或者JSONArray(和上面方法的区别是返回值是不一样的)
  4. JSONObject 与JSONArray

JSONObject:json对象 - 就是一个键对应一个值,使用{}

JSONArray:json数组 - 使用[],接收json对象

操作嵌套jsonAPI:

{"bookName":"海贼王","persons":[{"age":19,"name":"索隆","occupation":"剑士"}],"releaseData":"1997"}

jsonobject.getJSONArray("persons").getJSONObject(0).get("occupation").toString();
package com.tian.springbootdev.dev;


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

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class JsonDemo {

    public static void main(String[] args) {

        /**
         * JSONObject添加键值对,JSONArray添加json对象
         */
        JSONObject jsonObject = new JSONObject();
        JSONObject jsonObject1 = new JSONObject();

        jsonObject.put("name", "路飞");
        jsonObject.put("age", 18);
        jsonObject.put("occupation", "海贼");
        System.out.println(jsonObject);//{"occupation":"海贼","name":"路飞","age":18}

        jsonObject1.put("ship", "阳光号");

        JSONArray jsonArray = new JSONArray();
        jsonArray.add(jsonObject);
        jsonArray.add(jsonObject1);
        System.out.println(jsonArray);//[{"occupation":"海贼","name":"路飞","age":18},{"ship":"阳光号"}]

        //JSONObject嵌套
        jsonObject.put("message", jsonObject1);
        System.out.println(jsonObject);//{"occupation":"海贼","name":"路飞","message":{"ship":"阳光号"},"age":18}

        //---------------------------------------------------------------------------------------------------------

        //对象 - JSON
        /**
         * 序列化
         */
        JsonBook jsonBook = new JsonBook();
        jsonBook.setBookName("海贼王");
        jsonBook.setReleaseData("1997");

        List<Person> personList = new ArrayList<>();
        Person person = new Person();
        person.setAge(19);
        person.setName("索隆");
        person.setOccupation("剑士");
        personList.add(person);

        jsonBook.setPersons(personList);
        String bookText = JSON.toJSONString(jsonBook);
        System.out.println(bookText);//{"bookName":"海贼王","persons":[{"age":19,"name":"索隆","occupation":"剑士"}],"releaseData":"1997"}

        //操作json数据
        JSONObject jObject = JSON.parseObject(bookText);
        String occupation = jObject.getJSONArray("persons").getJSONObject(0).get("occupation").toString();
        System.out.println(occupation);//剑士

        /**
         * 反序列化
         */
        String personText1 = "{\"age\":18,\"name\":\"路飞\",\"occupation\":\"海贼\"}";
        Person person1 = JSON.parseObject(personText1, Person.class);
        System.out.println("age:" + person1.getAge() + "、name:" + person1.getName());//age:18、name:路飞

        /**
         *javabean 转 JSONObject
         */
        Person person2 = new Person("乔巴", "船医", 12);
        JSONObject jsonObject2 = (JSONObject) JSON.toJSON(person2);
        System.out.println(jsonObject2);//{"occupation":"船医","name":"乔巴","age":12}

        //---------------------------------------------------------------------------------------------------------

        /**
         * 数组 转 jsonArray字符串 ------- jsonArray字符串 转 JSONArray
         */
        //toJOSNString 接收一个Object对象
        String[] array = new String[]{"vivo", "ipnone", "oppo"};
        String arrString = JSON.toJSONString(array);
        System.out.println(arrString);//["vivo","ipnone","oppo"]

        JSONArray jsonArray1 = JSON.parseArray(arrString);
        System.out.println(jsonArray1);//["vivo","ipnone","oppo"]

        /**
         * list 转 json格式字符串
         */
        List<Person> personList1 = new ArrayList<>();

        Person per1 = new Person("乔巴", "船医", 12);
        Person per2 = new Person("路飞", "船长", 18);
        Person per3 = new Person("索隆", "剑士", 19);

        personList1.add(per1);
        personList1.add(per2);
        personList1.add(per3);

        String perString = JSON.toJSONString(personList1);
        System.out.println(perString);//[{"age":12,"name":"乔巴","occupation":"船医"},{"age":18,"name":"路飞","occupation":"船长"},{"age":19,"name":"索隆","occupation":"剑士"}]

        ////操作json数据 - 获取jsonArray中的值
        JSONArray jsonArray2 = JSON.parseArray(perString);
        String name = jsonArray2.getJSONObject(0).get("name").toString();
        System.out.println(name);//乔巴

        //---------------------------------------------------------------------------------------------------------

        /**
         * map 转 json格式字符串
         */
        Map<String,Object> map = new HashMap<>();
        map.put("per1",per1);
        map.put("per2",per2);
        map.put("per3",per3);
        String mapString = JSON.toJSONString(map);
        System.out.println(mapString);//{"per1":{"age":12,"name":"乔巴","occupation":"船医"},"per2":{"age":18,"name":"路飞","occupation":"船长"},"per3":{"age":19,"name":"索隆","occupation":"剑士"}}
        //格式化样式
        String mapString1 = JSON.toJSONString(map,true);

        /**
         * json格式字符串 转map
         */
        //第一种
        Map<String,Object> map1 = JSON.parseObject(mapString1,Map.class);
        //第二种
        Map<String,Object> map2 =  JSON.parseObject(mapString1);
        System.out.println("map:" + map1.get("per1"));//map:{per1={"occupation":"船医","name":"乔巴","age":12}, per2={"occupation":"船长","name":"路飞","age":18}, per3={"occupation":"剑士","name":"索隆","age":19}}



    }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值