fastjson的使用

jopen 10年前發布 | 12K 次閱讀 fastjson JSON開發包

一、項目結構

這里寫圖片描述

一個學生類,其中學生類中可以包含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);
}</pre><br />

[
    {
        "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());
}</pre><br />
 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!