歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> 阿裡巴巴fastjson的使用

阿裡巴巴fastjson的使用

日期:2017/3/1 9:27:30   编辑:Linux編程

阿裡巴巴fastjson的使用

一、項目結構

一個學生類,其中學生類中可以包含Course類對象

二、數據對象轉化為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);
    }
  • 1
[
    {
        "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"}]
  • 1
    @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());
    }

Json應用案例之FastJson http://www.linuxidc.com/Linux/2015-03/115366.htm

FastJson庫省略小數點後0的Bug的跟蹤 http://www.linuxidc.com/Linux/2015-06/118522.htm

fastjson 的詳細介紹:請點這裡
fastjson 的下載地址:請點這裡

Copyright © Linux教程網 All Rights Reserved