无需 JsonObject 和 Array 即可将 XML 转换为 java 中的 JSON 的高效解析器
Efficient parser to convert XML to JSON in java without JsonObject and Array
我试图在 java 中将 XML 转换为 JSON。我正在使用 Stax 解析器进行转换。得到START_ELEMENT、END_ELEMENT、CHARACTERS后,不知如何转换成JSON格式。不使用任何内置库、JsonObject、JSonArray.IS 此转换可用的任何逻辑...
为什么不使用现有的 JAR 来完成这项工作。
java-json 中就有这样一种高效的解析器
http://www.java2s.com/Code/Jar/j/Downloadjavajsonjar.htm
可以使用
在一行中完成转换
import org.json.XML;
....
JSONObject jsonObject = XML.toJSONObject("Your XML Here");
Underscore-java library can convert xml to json. I am the maintainer of the project. Live example
import com.github.underscore.U;
public class JsonConversion {
public static String TEST_XML_STRING =
"<?xml version=\"1.0\" ?><test attrib=\"moretest\">Turn this to JSON</test>";
public static void main(String args[]) {
String jsonPrettyPrintString = U.xmlToJson(TEST_XML_STRING);
System.out.println(jsonPrettyPrintString);
// {
// "test": {
// "-attrib": "moretest",
// "#text": "Turn this to JSON"
// }
// }
}
}
将 XML 转换为 JSON 的示例代码。在 Eclipse 中创建一个 java 项目,因为我正在创建一个 java 项目,我将通过右键单击项目和 select 手动导入 Java-json.jar通过 select 添加外部 jar 选项添加 jar 文件和 运行 项目来配置构建路径。如果您使用 Maven 项目构建,请在 pom.xml
中添加以下依赖项
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20140107</version>
package com.test.jsontoxml
import org.json.JSONException;
import org.json.JSONObject;
import org.json.XML;
public class JsonConversion {
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());
}
}
}
我试图在 java 中将 XML 转换为 JSON。我正在使用 Stax 解析器进行转换。得到START_ELEMENT、END_ELEMENT、CHARACTERS后,不知如何转换成JSON格式。不使用任何内置库、JsonObject、JSonArray.IS 此转换可用的任何逻辑...
为什么不使用现有的 JAR 来完成这项工作。
java-json 中就有这样一种高效的解析器
http://www.java2s.com/Code/Jar/j/Downloadjavajsonjar.htm
可以使用
在一行中完成转换import org.json.XML;
....
JSONObject jsonObject = XML.toJSONObject("Your XML Here");
Underscore-java library can convert xml to json. I am the maintainer of the project. Live example
import com.github.underscore.U;
public class JsonConversion {
public static String TEST_XML_STRING =
"<?xml version=\"1.0\" ?><test attrib=\"moretest\">Turn this to JSON</test>";
public static void main(String args[]) {
String jsonPrettyPrintString = U.xmlToJson(TEST_XML_STRING);
System.out.println(jsonPrettyPrintString);
// {
// "test": {
// "-attrib": "moretest",
// "#text": "Turn this to JSON"
// }
// }
}
}
将 XML 转换为 JSON 的示例代码。在 Eclipse 中创建一个 java 项目,因为我正在创建一个 java 项目,我将通过右键单击项目和 select 手动导入 Java-json.jar通过 select 添加外部 jar 选项添加 jar 文件和 运行 项目来配置构建路径。如果您使用 Maven 项目构建,请在 pom.xml
中添加以下依赖项<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20140107</version>
package com.test.jsontoxml
import org.json.JSONException;
import org.json.JSONObject;
import org.json.XML;
public class JsonConversion {
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());
}
}
}