java 在java中将xml转换为json
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16938958/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert xml to json in java
提问by user2054314
I want to convert from xml to json in java and get an output. But whike doing so only some parts from the xml are getting converted and not the whole xml. Any help My input xml is :
我想在java中从xml转换为json并获得输出。但是这样做只是 xml 中的某些部分被转换,而不是整个 xml。任何帮助我的输入 xml 是:
<?xml version="1.0" encoding="UTF-8"?>
<important-data certified="true" processed="true">
<timestamp>232423423423</timestamp>
<authors>
<author>
<firstName>Tim</firstName>
<lastName>Leary</lastName>
</author>
</authors>
<title>Flashbacks</title>
<shippingWeight>1.4 pounds</shippingWeight>
<isbn>978-0874778700</isbn>
</important-data>
And my code is: package com.discursive.answers;
我的代码是:包 com.discursive.answers;
import java.io.IOException;
import java.io.InputStream;
import org.apache.commons.io.IOUtils;
import net.sf.json.JSON;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;
import net.sf.json.xml.XMLSerializer;
import org.json.XML;
public class ConvertXMLtoJSON {
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) {
new ConvertXMLtoJSON();
}
public ConvertXMLtoJSON() {
try {
InputStream is = ConvertXMLtoJSON.class.getResourceAsStream("sample-xml.xml");
if (is != null) {
String xml = IOUtils.toString(is);
JSON json = XMLSerializer.readObject(xml);
System.out.println(json.toString().split(",").length);
for(int i= 0 ;i < json.toString().split(",").length; i ++)
System.out.println(json.toString().split(",")[i]);
} else {
System.out.println("Checkpoint 1");
}
} catch (java.io.IOException ioe) {
ioe.printStackTrace();
}
}
Applying the above mentioned xml and code I get the output as:
应用上面提到的 xml 和代码,我得到的输出为:
{"timestamp":"232423423423"
"authors":"\n\t\t\n\t\t\tTim\n\t\t\tLeary\n\t\t\n\t"
"title":"Flashbacks"
"isbn":"978-0874778700"
"shippingWeight":"1.4 pounds"}
where as it should be as :
它应该在哪里:
{
"@certified": "true",
"@processed": "true",
"timestamp": "232423423423",
"authors": [ {
"firstName": "Tim",
"lastName": "Leary"
}],
"title":
"Flashbacks",
"shippingWeight": "1.4 pounds",
"isbn": "978-0874778700"
}
What changes should I do to get the required output?
我应该做哪些更改才能获得所需的输出?
回答by osborp
The article How to Convert XML to JSON in Javagives the same example as yours using json-lib.
文章How to Convert XML to JSON in Java给出了与您使用json-lib相同的示例。
The code in the example works, so it might be worth checking the versions on your libs. The json-lib website lists the required libs.
示例中的代码有效,因此可能值得检查您的库中的版本。json-lib 网站列出了所需的库。
I managed to get mine working with the following libs on my build path:
我设法在我的构建路径上使用以下库:
- commons-beanutils-1.8.3
- commons-collections-3.2.1
- commons-io-1.2
- commons-lang-2.4
- commons-logging-1.1.3
- ezmorph-1.0.6
- json-lib2.4-jdk15
- xom-1.2.10
- commons-beanutils-1.8.3
- commons-collections-3.2.1
- commons-io-1.2
- commons-lang-2.4
- commons-logging-1.1.3
- ezmorph-1.0.6
- json-lib2.4-jdk15
- xom-1.2.10