如何将xml文件转换为json字符串并保留属性

时间:2023-02-08 21:47:40

I'm trying to convert a xml file to json and backwards, but when doing so the integrity changes, from:

我正在尝试将xml文件转换为json和向后,但是当这样做时,完整性会发生变化,来自:

<option value="0"> <!--something--> </option>

to

<option> <!--something--> <value>0</value> </option>

I get this when using org.json, is there another json library that can do this job while keeping file integrity?

我在使用org.json时得到这个,是否有另一个json库可以在保持文件完整性的同时完成这项工作?

1 个解决方案

#1


-3  

XML to Json

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 to xml :

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);            
    }

} 

#1


-3  

XML to Json

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 to xml :

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);            
    }

}