Java将Map数据转换为JSON对象数据

    xiaoxiao2024-11-20  41

    //pom.xml导入jar包 <!--拼接json--> <dependency> <groupId>net.sf.json-lib</groupId> <artifactId>json-lib</artifactId> <version>2.4</version> <classifier>jdk15</classifier> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.1</version> </dependency> <dependency> <groupId>commons-beanutils</groupId> <artifactId>commons-beanutils</artifactId> <version>1.8.0</version> </dependency> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.1.1</version> </dependency> <dependency> <groupId>commons-collections</groupId> <artifactId>commons-collections</artifactId> <version>3.2.1</version> </dependency> <dependency> <groupId>net.sf.ezmorph</groupId> <artifactId>ezmorph</artifactId> <version>1.0.6</version> </dependency> </dependencies>

     将获取到的数据存放在map中。然后通过JSONObject.fromObject()进行转换。如果map是空的则会报错。

    Map map = new HashMap(); map.put("realNumber",realNumber); JSONObject jsonObject = JSONObject.fromObject(map);

     

    最新回复(0)