Phase 1: parse JSON and parse JSON

Source: Internet
Author: User

Phase 1: parse JSON and parse JSON

"Phase 1" means the first time I systematically studied Android development. This is mainly to record my learning process.

 

Recently I learned to parse network data in JSON format, and the job also asked me to find an API address for the weather forecast, and then parse the data in JSON format. It may be because you are looking for a free API address. In Baidu APIStore, it returns the sample data in JSON format, which is different from the data displayed in Log after I obtain it, this is just a bit of a joke. In addition, it is recommended that beginners go to the "aggregate data" website to check the Sample Code. The Code is well written and the ideas and annotations are clear.

For the first time, JSON is parsed, and the data of this API is quite complicated. when parsing and reading the API, it is completely written. So I will share with you how to understand the JSON parsing method and how to obtain the data. The data format is from simple to complex. The basic syntax is detailed in Baidu encyclopedia.

The following analysis is based on this assumption that the requested data is converted to the String type and saved in the content Variable.

1. {"resultcode": "200", "reason": "query successful! "}

It is best to understand this situation, as longJSONObject response = new JSONObject (content)AndString result = response. getString ("resultcode ")In my understanding, a pair of curly braces {} represents an object, soResponseThis is the dataJSONObjectWhen this object is implemented, it is removed as the curly braces {}. This is helpful for understanding complicated data or. You can callGetMethod.

2. {"resultcode": "200", "reason": "query successful! "," Future ": [{" temperature ":" 28 ℃ ~ 36℃ "," weather ":" clear to cloudy "}]}

When the "value" is in square brackets [] (similar to the array), it must be called by an object of the upper level.GetJSONArray ("future ")Here isResponseWhen this step is performed, square brackets can also be removed. In square brackets [], each pair of curly braces {} also represents an object. It also has a subscript, which also starts from 0.

The code for obtaining the temperature is:

JSONObject response = new JSONObject (content); // ==> "Resultcode": "200", "reason": "query successful! "," Future ": [{" temperature ":" 28 ℃ ~ 36℃ "," weather ":" clear to cloudy "}]

JSONArray Future= Response.GetJSONArray ("future "); // ==>{ "Temperature": "28 ℃ ~ 36℃ "," weather ":" clear to cloudy "}

JSONObjectFutureObject=Future.GetJSONObject (0); // ==> "Temperature": "28 ℃ ~ 36℃ "," weather ":" clear to cloudy"

String temperature =FutureObject. GetString ("Temperature");

3. {"resultcode": "200 ",

"Reason": "query successful! ",

"Today": {"city": "Tianjin", "date_y": "March 21, 2014", "week": "Friday", "temperature": "8 ℃ ~ 20℃ "," weather ":" Clear Haze "," weather_id ": {" fa ":" 00 "," fb ":" 53 "}},

"Future": [{"temperature": "28 ℃ ~ 36℃ "," weather ":" clear to cloudy "," weather_id ": {" fa ":" 00 "," fb ":" 01 "}," wind ": "Nanfeng Grade 3-4", "week": "Monday", "date": "20140804 "},

{"Temperature": "28 ℃ ~ 36℃ "," weather ":" clear to cloudy "," weather_id ": {" fa ":" 00 "," fb ":" 01 "}," wind ": "southeast wind 3-4", "week": "Tuesday", "date": "20140805 "}

]

}

(1) read "city" in "today"

JSONObject response = new JSONObject (content); // ==> Same as above

JSONObject today = reponse. getJSONObject ("Today"); // => "City": "Tianjin", "date_y": "March 21, 2014", "week": "Friday", "temperature": "8 ℃ ~ 20℃ "," weather ":" Clear Haze "," weather_id ": {" fa ":" 00 "," fb ":" 53 "}

String city =Today. GetString ("City");

(2) read "fa" of "weather_id" of the second "temperature" of "future"

JSONObject response = new JSONObject (content); // ==> Same as above

JSONArray Future= Response.GetJSONArray ("future ");

JSONObjectFutureObject=Future.GetJSONObject (1); // ==> "Temperature": "28 ℃ ~ 36℃ "," weather ":" clear to cloudy "," weather_id ": {" fa ":" 00 "," fb ":" 01 "}," wind ": "southeast wind 3-4", "week": "Tuesday", "date": "20140805"

JSONObject weather_id = futureObject. getJSONObject ("weather_id "); // => "Fa": "00", "fb": "01"

String id =Weather_id. GetString ("Fa");

Summary: For JSON parsing, I understand it as a process of constantly removing brackets. First, find the data you want to obtain, and then peel it from the outermost layer. GetJSONObect (except for the outermost layer, the outermost layer is new JSONObject, which is also the first step), and getJSONArray is the one with square brackets. The data can be read until there is no parentheses in the position of the value ..

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.