
JSON 是一种轻量级的数据交换格式,JSON 的格式类似于键值对。我们可以使用org.json.XML类将XML转换为JSON数组,这提供了一个静态方法,XML.toJSONObject() 将 XML 转换为 JSON 数组。
Easily find JSON paths within JSON objects using our intuitive Json Path Finder
30
public static JSONObject toJSONObject(java.lang.String string) throws JSONException
在下面的示例中,将 XML 转换为 JSON 数组
import org.json.*;
public class ConvertXMLToJSONArrayTest {
public static String xmlString= <strong>"<!--?xml version="1.0" ?--></strong><?xml version=\"1.0\" ?><root><test attrib=\"jsontext1\">tutorialspoint</test><test attrib=\"jsontext2\">tutorix</test></root>";
public static void main(String[] args) {
try {
JSONObject json = XML.toJSONObject(xmlString); // converts xml to json
String jsonPrettyPrintString = json.toString(4); // json pretty print
System.out.println(jsonPrettyPrintString);
} catch(JSONException je) {
System.out.println(je.toString());
}
}
}{"root": {"test": [
{
"attrib": "jsontext1",
"content": "tutorialspoint"
},
{
"attrib": "jsontext2",
"content": "tutorix"
}
]}}以上就是如何在Java中将XML转换为JSON数组?的详细内容,更多请关注php中文网其它相关文章!
java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号