java中将Map集合转成json格式的数据

我们需要加载jar包或者直接使用maven管理中导入以下工具包

 <!--map转换json-->
        <dependency>
            <groupId>net.sf.json-lib</groupId>
            <artifactId>json-lib</artifactId>
            <version>2.4</version>
            <classifier>jdk15</classifier>
        </dependency>
        <dependency>
            <groupId>commons-beanutils</groupId>
            <artifactId>commons-beanutils</artifactId>
            <version>1.7.0</version>
        </dependency>
        <dependency>
            <groupId>commons-collections</groupId>
            <artifactId>commons-collections</artifactId>
            <version>3.1</version>
        </dependency>
        <dependency>
            <groupId>commons-lang</groupId>
            <artifactId>commons-lang</artifactId>
            <version>2.5</version>
        </dependency>
        <dependency>
            <groupId>net.sf.ezmorph</groupId>
            <artifactId>ezmorph</artifactId>
            <version>1.0.3</version>
        </dependency>
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.2</version>
        </dependency>

转换的代码:

//创建一个map集合的对象
Map<String, Object> map = new HashMap<String, Object>();
        map.put("msg_id","1234");
        map.put("lib_id",1);
        map.put("img_id", "a975b317-5e20-488b-a67f-050147983c09");
        //将map集合中的数据抓换成json格式
        JSONObject jsonMap = JSONObject.fromObject(map);
        System.out.print("打印转换的数据:"+" "+jsonMap)

在这里插入图片描述
为了方便看数据直接打印出来把数据,Map集合中的键值对数据直接都是使用 “=” 号进行连接的
json数据都是 “:” 连接的
在这里插入图片描述

发布了81 篇原创文章 · 获赞 26 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/weixin_44411569/article/details/100007157