004.fastjson方式

package com.jdk.json;

import com.alibaba.fastjson.JSON;
import com.jkd.entity.School;

public class Fastjson_Json
{
    public static String jsonString = "{\n" +
            "    \"name\": \"teacher\",\n" +
            "    \"computer\": {\n" +
            "        \"cpu\": \"intel7\",\n" +
            "        \"disk\": \"512G\"\n" +
            "    },\n" +
            "    \"students\": [\n" +
            "        {\n" +
            "            \"name\": \"张三\",\n" +
            "            \"age\": 18,\n" +
            "            \"sex\": \"男\"\n" +
            "        },\n" +
            "        {\n" +
            "            \"name\": \"李四\",\n" +
            "            \"age\": 19,\n" +
            "            \"sex\": \"男\"\n" +
            "        }\n" +
            "    ]\n" +
            "}\n";


    public static void main(String[] args)
    {
        //Json转换为java对象
        School school = JSON.parseObject(jsonString, School.class);
        System.out.println(school);


        //java对象转换为Json
        String s = JSON.toJSONString(school);
        System.out.println(s);

        //注意:fastjson要求JSON串中属性名一定要是小写

    }





}

 

posted @ 2021-09-01 09:59  李林林  阅读(31)  评论(0编辑  收藏  举报