JSONUtil工具类简单使用

一、在pom文件中引入依赖

<!--JSONutil工具类依赖-->
    <dependency>
        <groupId>cn.hutool</groupId>
        <artifactId>hutool-all</artifactId>
        <version>5.3.4</version>
    </dependency>

二、编写测试类进行测试

package java1.com.sinosoft.microservices.test;


import cn.hutool.json.JSONUtil;
import org.junit.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.HashMap;

@SpringBootTest
public class Test05 {



    @Test
    public void test05(){
        HashMap<String, Object> map = new HashMap<>();
        map.put("name","joon");
        map.put("age",18);
        String JsonString = JSONUtil.parse(map).toString();

        System.out.println("map = " + map);
         //输出结果:map = {name=joon, age=18}
        System.out.println("JsonString = " + JsonString);
        //输出结果:JsonString = {"name":"joon","age":18}
        
    }

}

由以上输出结果可知,通过JSONUtil.parse(map)将对象转换为一个JSON对象,然后再调用.toString方法将JSON对象转换为一个JSON字符串。

猜你喜欢

转载自blog.csdn.net/weixin_71921932/article/details/131399961