FastJson的使用

FastJson的使用

1.在pom中引入fastjson依赖

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

2.使用fastjson

fastjson的使用主要有三个对象

  • JSON
  • JSONObject
  • JSONArray

JSONObject和JSONArray继承自JSON

2.1JSON对象

JSON这个类主要用于转换:

  1. 将java对象序列化为json字符串
  2. 将JSON字符串反序列化为java对象

这三个方法用的比较多:

  • parseObject(String text, Class<T> clazz)
  • parseArray(String text, Class<T> clazz)
  • toJSONString(Object object)

2.2JSONObject

JSONObject继承了JSON,实现了Map接口,跟使用Map没有太多的区别,常用方法

  • getString(String key)
  • put(String key,Object value)
  • remove(Object key)

2.3 JSONArray

JSONArray是JSON数组,JSON数组对象中存储的是一个个JSON对象,所以类中的方法主要用于直接操作JSON对象,常用方法

  • getJSONObject(int index)
  • add(Object e)

3.fastjson常用方式实例

3.1Map转JSON字符串

 public void MaptoJson(){
     HashMap<String, String> map = new HashMap<>();
     map.put("key1","1");
     map.put("key2","2");
     String s = JSON.toJSONString(map);
     System.out.println("Map转Json:"+s);
 }

在这里插入图片描述

3.2JSONObject

public void JSONObject(){
JSONObject jsonObject = new JSONObject();
List<Object> array = new ArrayList<>();
array.add("111111");
sonObject.put("key1","1");
jsonObject.put("key2","2");
jsonObject.put("array",array);
jsonObject.toString();
System.out.println("测试JSONObjet:"+jsonOb
}

在这里插入图片描述

3.3POJO List转JSON字符串

POJO一般叫做简单的Java对象。

public void test6() {
    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);
}

在这里插入图片描述

3.4Json字符串转JsonObject

@Test
public void JsonToJsonObject() {
    String jsonStr = "{\"key1\":\"One\",\"key2\":\"110\"}";
    JSONObject jsonObject = JSONObject.parseObject(jsonStr);
    System.out.println(jsonObject.getString("key1"));
    System.out.println(jsonObject.getInteger("key2"));
    System.out.println(jsonObject);
}

在这里插入图片描述

3.5JsonObject转Json字符串

public void test3() {
    String jsonStr = "{\"key1\":\"One\",\"key2\":\"110\"}";
    JSONObject jsonObject = JSONObject.parseObject(jsonStr);
    System.out.println(jsonObject.getString("key1"));
    System.out.println(jsonObject.getInteger("key2"));
    String parserJsonStr = JSONObject.toJSONString(jsonObject);
    System.out.println(parserJsonStr);
}

在这里插入图片描述

3.6JSONArray 添加 JSONObject

@Test
public void JsonObjectInJsonArray() {
    JSONObject jsonObject1 = new JSONObject();
    jsonObject1.put("name", "张三");
    jsonObject1.put("age", 25);
 
    JSONObject jsonObject2 = new JSONObject();
    jsonObject2.put("name", "李四");
    jsonObject2.put("age", 28);
 
    JSONArray jsonArray = new JSONArray();
    jsonArray.add(jsonObject1);
    jsonArray.add(jsonObject2);
 
    String jsonArrStr = JSONArray.toJSONString(jsonArray);
    System.out.println(jsonArrStr);
}

在这里插入图片描述

3.7Json数组字符串转JsonArray

@Test
public void jsonStrToJsonArray() {
    String jsonArrStr = "[{\"name\":\"张三\",\"age\":25},{\"name\":\"李四\",\"age\":28}]";
    JSONArray jsonArray = JSONArray.parseArray(jsonArrStr);
    for (Object object : jsonArray) {
        JSONObject jsonObject = (JSONObject) object;
        System.out.println(jsonObject.getString("name"));
        System.out.println(jsonObject.getString("age"));
    }
}

在这里插入图片描述

3.8POJO转JsonStr

@Test
public void PojoToJsonStr() {
    Person person1 = new Person();
    person1.setName("张三");
    person1.setAge(26);
    person1.setBirthday(new Date());
    String object = JSONObject.toJSONString(person1);
    System.out.println(object);

}

在这里插入图片描述

3.9 POJO 转 JSONObject

@Test
public void PojoToJsonObject{
    Person person1 = new Person();
    person1.setName("张三");
    person1.setAge(28);
    person1.setBirthday(new Date()); 
    /**方式一*/
    String jsonStr = JSONObject.toJSONString(person1);
    JSONObject jsonObject = JSONObject.parseObject(jsonStr);
    System.out.println(jsonObject.get("name"));
    /**方式二*/
    JSONObject jsonObject1 = (JSONObject)JSONObject.toJSON(person1);
    System.out.println(jsonObject1.get("age"));
}

在这里插入图片描述

3.10PojoList转JsonArray

@Test
public void test9() {
    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"));
 	System.out.println(jsonArray);
    /**方式2*/
    JSONArray jsonArray1 = (JSONArray)JSONArray.toJSON(persons);
    JSONObject jsonObject2 = (JSONObject)jsonArray1.get(1);
    System.out.println(jsonObject2.get("name"));
}

在这里插入图片描述

3.11JSON字符串 转 Map 与 POJO

@Test
    public void JsonToMap() {
        String content = "{\"PARENT_ID\":1,\"NAME\":\"国家级正职\",\"CODE\":\"101\",\"LEVEL_NUM\":2,\"ID\":2}";
        Map map = JSONObject.parseObject(content, Map.class);//json 对象转 map
        System.out.println(map);//输出:{CODE=101, LEVEL_NUM=2, ID=2, PARENT_ID=1, NAME=国家级正职}
    }

在这里插入图片描述

3.12将json对象转JavaBean

@Test
public void jsonToJavaBean() {
    Person person1 = new Person();
    person1.setName("张三");
    person1.setAge(28);
    person1.setBirthday(new Date());
 
    String jsonPOJOStr = JSON.toJSONString(person1);
    Person person = JSONObject.parseObject(jsonPOJOStr, Person.class);
    System.out.println(person);
}

在这里插入图片描述

3.13Json字符串转MapList

 @Test
    public void JsonToList() {
        String content = "[{\"PARENT_ID\":1,\"NAME\":\"国家级正职\",\"CODE\":\"101\",\"LEVEL_NUM\":2,\"ID\":2},{\"PARENT_ID\":1,\"NAME\":\"国家级副职\",\"CODE\":\"102\",\"LEVEL_NUM\":2,\"ID\":3}]";
        //parseArray(String text, Class<T> clazz):clazz 指定 list 中的元素类型
        List<Map> mapList = JSONArray.parseArray(content, Map.class);//json 转 List<Map>
        System.out.println(mapList);
    }

在这里插入图片描述

3.14Json字符串转POJOList

@Test
public void JsonToPersonList() {
    String jsonArrPOJOStr = "[{\"birthday\":1530512954968,\"name\":\"张三\",\"age\":28}," +
            "{\"birthday\":1530512954968,\"name\":\"李四\",\"age\":25}]";
    List<Person> personList = JSONArray.parseArray(jsonArrPOJOStr, Person.class);
    for (Person person : personList) {
        System.out.println(person);
    }

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值