我正在尝试将 xml 文件转换为 json 并向后转换,但是这样做时完整性会发生变化,从:
<option value="0">
<!--something-->
</option>
至
<option>
<!--something-->
<value>0</value>
</option>
我在使用 org.json 时得到了这个,是否有另一个 json 库可以在保持文件完整性的同时完成这项工作?
有一个带有静态方法的underscore-javaU.xmlToJson(xml)
库。
<option value="0">
<!--something-->
</option>
输出:
{
"option": {
"-value": "0",
"#comment": "something"
},
"#omit-xml-declaration": "yes"
}
XML 转 Json
import net.sf.json.JSONObject;
import org.json.JSONObject;.
public class Main {
public static int PRETTY_PRINT_INDENT_FACTOR = 4;
public static String TEST_XML_STRING ="ur xml";
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());
}
}
}
json 到 xml:
import org.json.JSONException;
import org.json.JSONObject;
import org.json.XML;
public class jsontoxml {
public static void main(String[] args) throws JSONException {
String str="ur json here";
JSONObject json = new JSONObject(str);
String xml = XML.toString(json);
System.out.println(xml);
}
}