阿里巴巴fastjson的使用

一、项目结构

这里写图片描述

一个学生类,其中学生类中可以包含Course类对象
使用Maven管理项目的可以添加fastjson的坐标:

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

二、数据对象转化为Json字符串

GenerateJson.java代码标识转化为json字符串
(1)将学生对象转化为json,其中学生中包含Course对象

@Test
    public void testSimpleJSON(){
        Student stu = new Student("xuliugen", "nan", "123123", "100");
        Course course = new Course("JAVA", "xiaobin", "100");
        stu.setCourse(course);
        String json = JSON.toJSONString(stu);
        System.out.println(json);
    }
{
    "course":{
        "coursename":"JAVA",
        "coursescore":"100",
        "courseteacher":"xiaobin"
    },
    "password":"123123",
    "score":"100",
    "sex":"nan",
    "username":"xuliugen"
}

(2)将一个单独的实体对象转化为json

@Test
    public void testListJSON(){
        JSONTest jt1 = new JSONTest("xuliugen", "nan");
        JSONTest jt2 = new JSONTest("xieyan", "nv");
        List<JSONTest> li = new ArrayList<JSONTest>();
        li.add(jt1);
        li.add(jt2);
        String jsonstr = JSON.toJSONString(li);
        System.out.println(jsonstr);
    }
[{"name":"xuliugen","sex":"nan"},{"name":"xieyan","sex":"nv"}]

(3)将包含多个类似于(1)中的实体对象转化为json

@Test
    public void testMulJSON(){
        Student stu = new Student("xuliugen", "nan", "123123", "100");
        Course course = new Course("JAVA", "xiaobin", "100");
        stu.setCourse(course);
        Student stu2 = new Student("xieyan", "nan", "123123", "100");
        Course course2 = new Course("music", "qwe", "100");
        stu2.setCourse(course2);

        List<Student> stuList = new ArrayList<Student>();
        stuList.add(stu);
        stuList.add(stu2);
        String json2 = JSON.toJSONString(stuList);
        System.out.println(json2);
    }
[
    {
        "course":{
            "coursename":"JAVA",
            "coursescore":"100",
            "courseteacher":"xiaobin"
        },
        "password":"123123",
        "score":"100",
        "sex":"nan",
        "username":"xuliugen"
    },
    {
        "course":{
            "coursename":"music",
            "coursescore":"100",
            "courseteacher":"qwe"
        },
        "password":"123123",
        "score":"100",
        "sex":"nan",
        "username":"xieyan"
    }
]

三、解析json数据到实体对象

(1)解析上述(1)中学生中包含Course的对象

[{"name":"xuliugen","sex":"nan"},{"name":"xieyan","sex":"nv"}]
    @Test
    public void testParseSimpleJSON(){
        String json = "[{\"name\":\"xuliugen\",\"sex\":\"nan\"},{\"name\":\"xieyan\",\"sex\":\"nv\"}]";
        JSONArray jsonArray = JSON.parseArray(json);
        String str = jsonArray.getString(0);
        JSONTest jsonTest = JSON.parseObject(str,JSONTest.class);
        System.out.println(jsonTest.getSex());
    }
{
    "course":{
        "coursename":"JAVA",
        "coursescore":"100",
        "courseteacher":"xiaobin"
    },
    "password":"123123",
    "score":"100",
    "sex":"nan",
    "username":"xuliugen"
}

(2)由于只有一个对象,解析如下:

@Test
    public void testParseStudentIncludeCourseJSON() {
        String json = "{\"course\":{\"coursename\":\"JAVA\",\"coursescore\":\"100\",\"courseteacher\":\"xiaobin\"},\"password\":\"123123\",\"score\":\"100\",\"sex\":\"nan\",\"username\":\"xuliugen\"}";
        Student stu = JSON.parseObject(json,Student.class);
        System.out.println(stu.getPassword());
    }

(3)将上述中的(3)当有多个上述的对象的时候,解析如下:

[
    {
    "course":{
        "coursename":"JAVA",
        "coursescore":"100",
        "courseteacher":"xiaobin"
    },
    "password":"123123",
    "score":"100",
    "sex":"nan",
    "username":"xuliugen"
    },
    {
    "course":{
        "coursename":"music",
        "coursescore":"100",
        "courseteacher":"qwe"
    },
    "password":"123123",
    "score":"100",
    "sex":"nan",
    "username":"xieyan"
    }
]

解析如下:

@Test
    public void testParseListStudentIncludeCourseJSON() {
        String json = "[{\"course\":{\"coursename\":\"JAVA\",\"coursescore\":\"100\",\"courseteacher\":\"xiaobin\"},\"password\":\"123123\",\"score\":\"100\",\"sex\":\"nan\",\"username\":\"xuliugen123\"},{\"course\":{\"coursename\":\"music\",\"coursescore\":\"100\",\"courseteacher\":\"qwe\"},\"password\":\"123123\",\"score\":\"100\",\"sex\":\"nan\",\"username\":\"xieyan\"}]";

        JSONArray jsonArray = JSON.parseArray(json);
        String str = jsonArray.getString(0);
        Student stu = JSON.parseObject(str, Student.class);
        System.out.println(stu.getUsername());
    }
徐刘根 CSDN认证博客专家 分布式 MySQL 架构
微信关注公众号:Java后端技术(ID:JavaITWork),和20万人一起学习Java!

"Java后端技术"微信公众号专注Java相关技术:SSM、Spring全家桶、微服务、MySQL、MyCat、集群、分布式、中间件、Linux、网络、多线程,偶尔讲点运维Jenkins、Nexus、Docker、ELK,偶尔分享些技术干货,致力于Java全栈开发!
相关推荐
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页