使用net.sf.json包将xml字符串转为JSONObject:
引入maven依赖:
<dependency> <groupId>net.sf.json-lib</groupId> <artifactId>json-lib</artifactId> <version>2.2.2</version> <classifier>jdk15</classifier> </dependency>
Java代码:
import net.sf.json.xml.XMLSerializer; import net.sf.json.JSONArray; import net.sf.json.JSONObject; {
... String xml;//假设为xml字符串 XMLSerializer xmlSerializer = new XMLSerializer(); String resutStr = xmlSerializer.read(xml).toString(); JSONObject result = JSONObject.fromObject(resultStr);
...
}
原文:https://www.cnblogs.com/OnceAgain/p/9229243.html