JSONUtil tool class is easy to use

1. Introduce dependencies into the pom file

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

2. Write test classes for testing

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}
        
    }

}

As can be seen from the above output results, the object is converted into a JSON object through JSONUtil.parse(map), and then the JSON object is converted into a JSON string by calling the .toString method.

Supongo que te gusta

Origin blog.csdn.net/weixin_71921932/article/details/131399961
Recomendado
Clasificación