使用Java将xml转换为json [英] Convert xml to json with Java
本文介绍了使用Java将xml转换为json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有办法将xml文件转换为json? XML可以是任何结构,因此没有用于实例化的POJO类。我需要将xml转换为json或转换为没有根节点的Map。
Is there a way to convert a xml file to json? The XML can be of any structure, therefore there is no POJO class for instantiation. I need to convert the xml to json or to a Map, without root nodes.
例如:
<import name="person">
<item>
<firstName>Emil</firstName>
<lastName>Example</lastName>
<addresses>
<address>
<street>Example Blvd.</street>
</address>
<address>
<street>Example Ave.</street>
</address>
</addresses>
</item>
</import>
预期JSON
{
"firstName": "Emil",
"lastName": "Example",
"addresses": [
{ "street" : "Example Blvd." },
{ "street" : "Example Ave." }
]
}
推荐答案
import org.json.JSONException;
import org.json.JSONObject;
import org.json.XML;
XML.toJSONObject(xml_text).toString()
这篇关于使用Java将xml转换为json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文