fastjson的快速入门 01

废话不多说直接代码贴给你们

测试依赖


    junit
    junit
    4.12



    com.alibaba
    fastjson
    1.2.73


    org.projectlombok
    lombok
    1.18.24
 //对象转换json
    @org.junit.Test
    public void test1(){
        User user=new User("xzh","123");
        String s = JSON.toJSONString(user);
        System.out.println(s);
        //{"password":"123","username":"xzh"}
    }
    //List转换Json
    @org.junit.Test
    public void test2(){
        User user1=new User("xzh","123");
        User user2=new User("ZZ","456");
        List users=new ArrayList();
        users.add(user1);
        users.add(user2);
        String s = JSON.toJSONString(users);
        System.out.println(s);
        //[{"password":"123","username":"xzh"},{"password":"456","username":"ZZ"}]
    }
    //map转换JSON
    @org.junit.Test
    public void test3(){
        Map map=new HashMap();
        User user1=new User("xzh","123");
        User user2=new User("ZZ","456");
        map.put("user1",user1);
        map.put("user2",user2);
        String s = JSON.toJSONString(map);
        System.out.println(s);
        //{"user1":{"password":"123","username":"xzh"},"user2":{"password":"456","username":"ZZ"}}
    }
    //对象json的取值与对象反序列化
    @org.junit.Test
    public void test4(){
        //{"password":"123","username":"xzh"}
        String s="{\"password\":\"123\",\"username\":\"xzh\"}";
        JSONObject jsonObject = JSON.parseObject(s);
        System.out.println(jsonObject.getString("username"));
        //xzh
        System.out.println(jsonObject);
        //{"password":"123","username":"xzh"}
    }
    //list JSON的取值与反序列化
    @org.junit.Test
    public void test5(){
        //[{"password":"123","username":"xzh"},{"password":"456","username":"ZZ"}]
       String s="[{\"password\":\"123\",\"username\":\"xzh\"},{\"password\":\"456\",\"username\":\"ZZ\"}]";
        JSONArray jsonArray = JSON.parseArray(s);
        for (Object o : jsonArray) {
            System.out.println(o);
        }
        //{"password":"123","username":"xzh"}
        //{"password":"456","username":"ZZ"}
        System.out.println(jsonArray.getString(0));
        //{"password":"123","username":"xzh"}
        System.out.println(jsonArray);
        //[{"password":"123","username":"xzh"},{"password":"456","username":"ZZ"}]
    }
    //map Json取值和反序列化
    @org.junit.Test
    public void test6(){
        //{"user1":{"password":"123","username":"xzh"},"user2":{"password":"456","username":"ZZ"}}
        String s="{\"user1\":{\"password\":\"123\",\"username\":\"xzh\"},\"user2\":{\"password\":\"456\",\"username\":\"ZZ\"}}";
        JSONObject jsonObject = JSON.parseObject(s);
        JSONObject user11 = jsonObject.getJSONObject("user1");
        System.out.println(user11.getString("username"));
        //xzh
        String user1 = jsonObject.getString("user1");
        System.out.println(user1);
        //String user1 = jsonObject.getString("user1");
        System.out.println(jsonObject);
        //{"user1":{"password":"123","username":"xzh"},"user2":{"password":"456","username":"ZZ"}}
    }

你可能感兴趣的:(工具,junit,java,json)