Convert XML to JSON format

后端 未结 10 862
粉色の甜心
粉色の甜心 2020-12-13 07:43

I have to convert docx file format (which is in openXML format) into JSON format. I need some guidelines to do it. Thanks in advance.

相关标签:
10条回答
  • 2020-12-13 07:43

    If you need to be able to manipulate your XML before it gets converted to JSON, or want fine-grained control of your representation, go with XStream. It's really easy to convert between: xml-to-object, json-to-object, object-to-xml, and object-to-json. Here's an example from XStream's docs:

    XML
    <person>
      <firstname>Joe</firstname>
      <lastname>Walnes</lastname>
      <phone>
        <code>123</code>
        <number>1234-456</number>
      </phone>
      <fax>
        <code>123</code>
        <number>9999-999</number>
      </fax>
    </person>
    
    POJO (DTO)
    public class Person {
        private String firstname;
        private String lastname;
        private PhoneNumber phone;
        private PhoneNumber fax;
        // ... constructors and methods
    }
    
    Convert from XML to POJO:
    String xml = "<person>...</person>";
    XStream xstream = new XStream();
    Person person = (Person)xstream.fromXML(xml);
    
    And then from POJO to JSON:
    XStream xstream = new XStream(new JettisonMappedXmlDriver());
    String json = xstream.toXML(person);
    

    Note: although the method reads toXML() XStream will produce JSON, since the Jettison driver is used.

    0 讨论(0)
  • 2020-12-13 07:43

    I have come across a tutorial, hope it helps you. http://www.techrecite.com/xml-to-json-data-parser-converter

    0 讨论(0)
  • 2020-12-13 07:47

    The XML class in the org.json namespace provides you with this functionality.

    You have to call the static toJSONObject method

    Converts a well-formed (but not necessarily valid) XML string into a JSONObject. Some information may be lost in this transformation because JSON is a data format and XML is a document format. XML uses elements, attributes, and content text, while JSON uses unordered collections of name/value pairs and arrays of values. JSON does not does not like to distinguish between elements and attributes. Sequences of similar elements are represented as JSONArrays. Content text may be placed in a "content" member. Comments, prologs, DTDs, and <[ [ ]]> are ignored.

    0 讨论(0)
  • 2020-12-13 07:47

    If you have a valid dtd file for the xml snippet, then you can easily convert xml to json and json to xml using the open source eclipse link jar. Detailed sample JAVA project can be found here: http://www.cubicrace.com/2015/06/How-to-convert-XML-to-JSON-format.html

    0 讨论(0)
  • 2020-12-13 07:48

    There is no direct mapping between XML and JSON; XML carries with it type information (each element has a name) as well as namespacing. Therefore, unless each JSON object has type information embedded, the conversion is going to be lossy.

    But that doesn't necessarily matter. What does matter is that the consumer of the JSON knows the data contract. For example, given this XML:

    <books>
      <book author="Jimbo Jones" title="Bar Baz">
        <summary>Foo</summary>
      </book>
      <book title="Don't Care" author="Fake Person">
        <summary>Dummy Data</summary>
      </book>
    </books>
    

    You could convert it to this:

    {
        "books": [
            { "author": "Jimbo Jones", "title": "Bar Baz", "summary": "Foo" },
            { "author": "Fake Person", "title": "Don't Care", "summary": "Dummy Data" },
        ]
    }
    

    And the consumer wouldn't need to know that each object in the books collection was a book object.

    Edit:

    If you have an XML Schema for the XML and are using .NET, you can generate classes from the schema using xsd.exe. Then, you could parse the source XML into objects of these classes, then use a DataContractJsonSerializer to serialize the classes as JSON.

    If you don't have a schema, it will be hard getting around manually defining your JSON format yourself.

    0 讨论(0)
  • 2020-12-13 07:55

    Converting complete docx files into JSON does not look like a good idea, because docx is a document centric XML format and JSON is a data centric format. XML in general is designed to be both, document and data centric. Though it is technical possible to convert document centric XML into JSON, handling the generated data might be overly complex. Try to focus on the actual needed data and convert only that part.

    0 讨论(0)
提交回复
热议问题