dan*_*sky 167
Maven依赖:
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20180813</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
XML.java
是你正在寻找的课程:
import org.json.JSONObject;
import org.json.XML;
public class Main {
public static int PRETTY_PRINT_INDENT_FACTOR = 4;
public static String TEST_XML_STRING =
"<?xml version=\"1.0\" ?><test attrib=\"moretest\">Turn this to JSON</test>";
public static void main(String[] args) {
try {
JSONObject xmlJSONObj = XML.toJSONObject(TEST_XML_STRING);
String jsonPrettyPrintString = xmlJSONObj.toString(PRETTY_PRINT_INDENT_FACTOR);
System.out.println(jsonPrettyPrintString);
} catch (JSONException je) {
System.out.println(je.toString());
}
}
}
Run Code Online (Sandbox Code Playgroud)
输出是:
{"test": {
"attrib": "moretest",
"content": "Turn this to JSON"
}}
Run Code Online (Sandbox Code Playgroud)
Kri*_*hna 54
要将XML文件转换为JSON,请包含以下依赖项
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20140107</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
你可以在这里从Maven资源库下载Jar.然后实现为:
String soapmessageString = "<xml>yourStringURLorFILE</xml>";
JSONObject soapDatainJsonObject = XML.toJSONObject(soapmessageString);
System.out.println(soapDatainJsonObject);
Run Code Online (Sandbox Code Playgroud)