Go to JSON and XML transformations

Source: Internet
Author: User
Tags cdata

This two-day process of the work of the module, in the JSON and XML conversion has some strange problems, carefully, it is not known to the org.json.* package too little.
At night, Baidu, Google, to find out the cause of the problem.
In the module, using two methods, Xml2json () and Json2xml (), the main problem is the implementation of the two methods.
The Java classes that XML and JSON convert to each other are commonly used in org.json.* packages with Xml.java and Jsonml.java, and the first half of the time is spent in JSON format and XML generation
Format validation, JSON data string format is not wrong, but at the time of conversion need to add ";" to indicate the end. After half the time spent in formatting the match
, refer to the ToString (Jsonobject Jo) method of Xml.java and Jsonml.java API,JSONML, as described below:
Reverse the JSONML transformation, making an XML text from a jsonobject. The
Jsonobject must contain a "TagName" property. If it had children, then it must had a
"ChildNodes" property containing an array of objects. The other properties is attributes
with string values.
The ToString (Object O) method of Xml.java is described as follows: Convert a jsonobject into a well-formed, Element-normal XML
String. It is more generalized than JsonML's ToString () method, which can be expressed as "Batch_type": {"TYPE": "int", "Content": 1}.
If you use the JSONML class to process, the resulting JSON string is: {"ChildNodes": [1], "type": "int", "TagName": "Batch_type"}. As JSONML
ToString describes: The converted JSON object must contain the TagName attribute, and his child nodes must be represented in childnodes form, and the other attributes with the characters
The value of the string form.

Report:

The corresponding JSON string is obtained through XML,
private static string Xml2json (String xml) throws jsonexception{
Jsonobject job = Jsonml.tojsonobject (XML);
String rs = job.tostring ();

Return RS;
}


To convert the JSON string to XML,
private static string Json2xml (String jsonstr) throws jsonexception{
Jsonobject job = new Jsonobject (jsonstr+ ";");
String jsonxml = (string) jsonml.tostring (Job);
return jsonxml;
}

App version, platform version (Sdkversion), app version number (Versioncode): Be careful to distinguish between these fields. The app version is a version of the string that the developer provides to the user, such as 2.1.2, and the platform version is the SDK API supported by the application. The application version number is an integer whose value is greater than or equal to 0.

MD5 code for the resource bundle (PACKAGEMD5): contains only 0-9,a-f characters, in total. Make sure that the number of digits is correct and that the letter is lowercase

When Chinese is present, you need to use <! [cdata[Chinese]]> Please be careful not to lose <> write:! [cdata[Chinese]]

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.