json的序列化和反序列化

package test;

import com.alibaba.fastjson.JSON;
import org.junit.Test;
import test.Student;

import java.util.*;

import static com.alibaba.fastjson.JSON.parseArray;

public class testFastJson {

    public  Date getDate(){
        Date date = new Date();
        return date;
    }

    @Test
    //json格式字符串,反序列化为Map集合
    public void testJsonToMap(){
        String jsonStr = "{\"student2\":{\"address\":\"北京\",\"age\":18,\"birthday\":1714560509775,\"mail\":\"123.qq.com\",\"name\":\"小米\",\"sex\":\"男\"},\"student1\":{\"address\":\"北京\",\"age\":18,\"birthday\":1714560509775,\"mail\":\"123.qq.com\",\"name\":\"小米\",\"sex\":\"男\"}}\n";
        Map<String, Object> map = JSON.parseObject(jsonStr);
        System.out.println(map);
    }



    @Test
    // json格式字符串,反序列化为List集合
    public void testJsonToList(){
        String jsonStr = "[{\"address\":\"北京\",\"age\":18,\"birthday\":1714543419605,\"mail\":\"123.qq.com\",\"name\":\"小米\",\"sex\":\"男\"}, {\"address\":\"北京\",\"age\":18,\"birthday\":1714543419605,\"mail\":\"123.qq.com\",\"name\":\"小米\",\"sex\":\"男\"}]\n";

        List<Student> students =   JSON.parseArray(jsonStr, Student.class);

        for (Student student : students) {
            System.out.println(student);
        }

    }


    @Test
    //json格式字符串,反序列化为java对象
    public void testJsonToObject(){

      String jsonString = "{\"address\":\"北京\",\"age\":18,\"mail\":\"123.qq.com\",\"name\":\"小米\",\"sex\":\"男\"}";
       Student student1 = JSON.parseObject(jsonString, Student.class);
       System.out.println(student1);


    }


//    ====================================================================================

    @Test
    //map集合,序列化json格式的字符串
    public void testMapToJson(){
        Map<String, Student> map = new HashMap<>();

        Student student1 = new Student();
        student1.setName("小米");
        student1.setAge(18);
        student1.setSex("男");
        student1.setMail("123.qq.com");
        student1.setAddress("北京");
        student1.setBirthday(getDate());

        Student student2 = new Student();
        student2.setName("小米");
        student2.setAge(18);
        student2.setSex("男");
        student2.setMail("123.qq.com");
        student2.setAddress("北京");
        student2.setBirthday(getDate());

        map.put("student1", student1);
        map.put("student2", student2);
// json格式的字符串是对象,,对象中有2个键,,,键对应的值是Sttudent对象
//{"student2":{"address":"北京","age":18,"birthday":1714560509775,"mail":"123.qq.com","name":"小米","sex":"男"},
// "student1":{"address":"北京","age":18,"birthday":1714560509775,"mail":"123.qq.com","name":"小米","sex":"男"}}


        String str = JSON.toJSONString(map);

        
        System.out.println(str);

    }


    @Test
    //list集合,序列化json格式的字符串
    public void testListToJson(){

        List<Student> list = new ArrayList<>();
        Student student1 = new Student();
        student1.setName("小米");
        student1.setAge(18);
        student1.setSex("男");
        student1.setMail("123.qq.com");
        student1.setAddress("北京");
        student1.setBirthday(getDate());

        Student student2 = new Student();
        student2.setName("小米");
        student2.setAge(18);
        student2.setSex("男");
        student2.setMail("123.qq.com");
        student2.setAddress("北京");
        student2.setBirthday(getDate());

        list.add(student1);
        list.add(student2);

        //list集合,序列化json格式的字符串

        String str = JSON.toJSONString(list);

        //转换后的结果是数组,数组的元素是对象
//        [{"address":"北京","age":18,"birthday":1714543419605,"mail":"123.qq.com","name":"小米","sex":"男"},
//        {"address":"北京","age":18,"birthday":1714543419605,"mail":"123.qq.com","name":"小米","sex":"男"}]

        System.out.println(str);



    }

    @Test
    //java对象,序列化json格式的字符串
    public void testObjectToJson(){
        Student student = new Student();
        student.setName("小米");
        student.setAge(18);
        student.setSex("男");
        student.setMail("123.qq.com");
        student.setAddress("北京");
        student.setBirthday(getDate());

        String str = JSON.toJSONString(student);
//        {"address":"北京","age":18,"mail":"123.qq.com","name":"小米","sex":"男"}
//       {"address":"北京","age":18,"birthday":1714561073390,"mail":"123.qq.com","name":"小米","sex":"男"}
        System.out.println(str);

    }


}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值