JSON parsing and XML parsing differences contrast _android

Source: Internet
Author: User
Tags data structures mixed tag name tomcat server

JSON parsing and XML parsing are two common ways of parsing, in which the JSON-parsed market share is larger. This paper analyzes the differences between the two analytic methods, and prepares the data for better processing. Since the current phase is mainly mobile development, the JSON parsing and XML parsing described in this article are tested in an Android environment.

The first thing to be clear is that data parsing is to get every value of the data, similar to the form of a key-value pair for JSON data, a form of labels and values for XML. In short, you want to use a key or tag in a JSON or XML data to find the corresponding value.

The second is to look at the data structure of both types of data. JSON data is divided into three types: object type, array type, and mixed type. Object type JSON data is wrapped in curly braces, corresponding to attributes and values. Array type JSON data can be grouped together as multiple object types JSON data, wrapped in brackets. Mixed-type JSON data has both an object type and an array type, and curly braces represent objects, and brackets represent arrays. The XML data is stored as a label, and the value is obtained by the tag name. So whether it's JSON parsing or XML parsing, you can first define a two-column collection for storing data.

JSON data type:

There are two ways to parse JSON data in an Android environment, one of which is a custom method that is layered across data structures, and the other is an open source framework. When the custom method is parsed, it can be divided into object parsing, array parsing and comprehensive parsing. Object parsing is simpler, first creating a JSON object, using the object's GetXXX () method to get the corresponding value, and then storing it in the collection, that is, getting the JSON data. Array parsing first obtains the JSON object through the Jsonarray object and then references the method of the object resolution. The comprehensive analysis is done on the basis of the above two kinds of analysis. According to the structure of the JSON data, getting the JSON object and the Jsonarray object once in order is known to get the required data. The basic principle of parsing with open source framework is similar, just need to lead the package in advance, and create a method class to invoke the content in the package.

To read JSON data for an object type:

To read the JSON data for an array type:

Read the integrated type of JSON data:

To read the directory structure of JSON data using an open source framework:

XML parsing is a side-reading edge parsing, and the method is fixed, which is resolved from top to bottom. First create the Xmlpullparser parser and use the parser to set the stream object to resolve. Then in a loop to determine the label type, if you encounter the file end sign (end_document) Check out of the loop, otherwise enter the loop. In the cycle in two cases, if the start tag (Start_tag), into the label inside, based on the label name to determine the data to be obtained, when the signature and the name of the data to be obtained, with Nexttext () to obtain data. If the end tag (End_tag) stores the acquired data in the collection. Finally, don't forget to move the label to the next position, which is: type = Parser.next ().

Method code for XML parsing:

For better presentation, the final case for this article is to use the Android device to get JSON and XML data from the Tomcat server, and then parse it to display the parsed results on the Android device.

JSON raw DATA:

{"ResultCode": "Reason": "Query Success!", "result": {"SK": {"temp": "", "wind_direction": "Westerly", "Wind_strength": " Level 2 "," humidity ":" 4% "," Time ":" 14:25 "}," Today ":" The City ":" Tianjin "," date_y ":" March 21, 2014 "," Week ":" Friday "," Temperature " ":" "8℃~20℃", "Weather": "Clear to the haze", "weather_id": {"FA": "", "FB": "" "}," Wind ":" Southwest Breeze "," dressing_index ":" Colder "," D Ressing_advice ":" Recommended coats, coats and sweaters, clothes and other clothing. "," Uv_index ":" Medium "," Comfort_index ":" "," Wash_index ":" More Appropriate "," Travel_index ":" Appropriate "," Exercise_index ":" More Appropriate "," Dryin G_index ":" "}," Future ": [{" Temperature ":" 28℃~36℃ "," Weather ":" Clear to Cloudy "," weather_id ": {" FA ":" "", "FB": "", "WI nd ":" South 3-4 Level "," Week ":" Monday "," date ":" 20140804 "}, {" Temperature ":" 28℃~36℃ "," Weather ":" Clear to Cloudy "," weather_id ": {" FA ":"
"", "FB": "3-4"}, "Wind": "Southeast Wind", "Week": "Tuesday", "date": "20140805"}, {"Temperature": "27℃~35℃", "Weather": "Sunny Turn Cloudy", "weather_id": {"FA": "The", "FB": "3-4"}, "Wind": "Southeast Wind", "Week": "Wednesday", "date": "20140806"}, {"TemperatUre ":" 27℃~34℃ "," Weather ":" Cloudy "," weather_id ": {" FA ":" "", "FB": "", "Wind": "Southeast 3-4", "Week": "Thursday", "date": "201 40807 "}, {" Temperature ":" 27℃~33℃ "," Weather ":" Cloudy "," weather_id ": {" FA ":" A "," FB ":" A "}," Wind ":" The Northeast 4-5 "," Week " : "Friday", "date": "20140808"}, {"Temperature": "26℃~33℃", "Weather": "Cloudy", "weather_id": {"FA": "", "FB": "A"}, "win D ":" North Wind level 4-5, "Week": "Saturday", "date": "20140809"}, {"Temperature": "26℃~33℃", "Weather": "Cloudy", "weather_id": {"FA": "01" , "FB": "I"}, "Wind": "North 4-5 level", "Week": "Sunday", "date": "20140810"}}, "Error_code": 0}

Resolves the "future" data in which the results are as follows:

XML RAW DATA:

<?xml version= "1.0" encoding= "UTF-8"?> <channel> <item> <title> Army report Caihou </title> <
Description> people death account does not eliminate anti-corruption step non-stop, support, martial, top, there is hope.
</description> <image>http://192.168.1.103:8080/img/a.jpg</image> <type>1</type> <comment>163</comment> </item> <item> <title> female driver overturned and went straight to Mahjong room </title> < Description> female driver overturned and went straight to mahjong room, said survival luck must red </description> <image>http://192.168.1.103:8080/img/b.jpg</ image> <type>2</type> </item> <item> <title> young man when "male PR" thought accompany beauty </title> < Description> Source: China Youth network, young man when "male PR" thought to accompany the beautiful woman, after the post was Aunt scared </description> <image>http://192.168.1.103:8080/img/ 
c.jpg</image> <type>3</type> </item> <item> <title> men see girl figure want to rape </title> <description> Source: Beijing News, see face after giving up still arrested </description> <image>http://192.168.1.103:8080/img/d.jpg</ Image> <type>1</type> <comment>763</comment> </item> </channel> 

After the analysis of the results show:

So far, JSON parsing and XML parsing are basically done.

The above is a small set for you to introduce the JSON parsing and XML parsing differences in contrast, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.