Parsing Android json data (jsonReader) and jsonjsonreader

Source: Internet
Author: User
Tags tagname

Parsing Android json data (jsonReader) and jsonjsonreader

Json data parsing is relatively easy, and the Implementation Code is also very simple. Here the jsonReade method is used for json data parsing.

1. Before parsing, You Need To Know What json data is.

The objects stored in json data are unordered sets of "name/value" pairs. Compared with other data storage methods, json data has advantages in readability, scalability, encoding difficulty, and decoding difficulty. In json data,

For an object:

(1) An object starts with "{" (left parenthesis) and ends with "}" (right Parenthesis.

(2) Each "name" is followed by a ":" (colon );

(3) Use commas (,) to separate 'name/value' pairs.

For an array:

(1) An array starts with "[" (left brackets) and ends with "]" (right brackets.

(2) Use commas (,) to separate values.

The following is an example of a group of json data officially provided by android:

[   {     "id": 912345678901,     "text": "How do I read JSON on Android?",     "geo": null,     "user": {       "name": "android_newb",       "followers_count": 41      }     },   {     "id": 912345678902,     "text": "@android_newb just use android.util.JsonReader!",     "geo": [50.454722, -104.606667],     "user": {       "name": "jesse",       "followers_count": 2     }   } ]

 

In the code, if json data is defined directly, you must use \ escape in the code. The above json format in the Code is: (IN java code, create a piece of json data, "the symbol needs to be escaped)

    private String jsonDate = "["            + "{\"id\": 912345678901,"            + "\"text\":\"How do I read JSON on Android?\","            + "\"geo\":null,"            + "\"user\":{\"name\":\"android_newb\",\"followers_count\":41}},"            + "{\"id\": 912345678902,"            + "\"text\":\"@android_newb just use android.util.JsonReader!\","            + "\"geo\":[50.454722,-104.606667],"            + "\"user\":{\"name\":\"jesse\",\"followers_count\":2}}"            + "]";

1. Use the JsonReader method to parse Json Data Objects. You need to create a JsonReader object.

2. Use beginArray () to parse the first array on the left.

3. Use beginObject () to parse the first object in array.

4. You can directly obtain parsed data for direct data, but you need to write a Parsing Method for Data nested in arrays in json.

5. After parsing, do not forget to use endArray () and endObject () to close the resolution.

 

Package com. mecury. jsontest; import java. io. IOException; import java. io. stringReader; import android. util. jsonReader; import android. util. jsonToken; public class JsonUtils {public void parseJson (String jsonDate) throws IOException {// create JsonReader object JsonReader jsReader = new JsonReader (new StringReader (jsonDate); jsReader. beginArray (); while (jsReader. hasNext () {readMessage (jsReader);} jsReader. endA Rray ();} public void readMessage (JsonReader jsReader) throws IOException {jsReader. beginObject (); while (jsReader. hasNext () {String tagName = jsReader. nextName (); if (tagName. equals ("id") {System. out. println ("name:" + jsReader. nextLong ();} else if (tagName. equals ("text") {System. out. println ("text:" + jsReader. nextString ();} else if (tagName. equals ("geo") & jsReader. peek ()! = JsonToken. NULL) {readDoubleArray (jsReader);} else if (tagName. equals ("user") {readUser (jsReader);} else {// skip the current value jsReader. skipValue (); System. out. println ("skip ======>") ;}} jsReader. endObject ();} // parse the data in geo public void readDoubleArray (JsonReader jsReader) throws IOException {jsReader. beginArray (); while (jsReader. hasNext () {System. out. println (jsReader. nextDouble ();} jsReader. endArray () ;}// read data from the user public void readUser (JsonReader jsReader) throws IOException {String userName = null; int followsCount =-1; jsReader. beginObject (); while (jsReader. hasNext () {String tagName = jsReader. nextName (); if (tagName. equals ("name") {userName = jsReader. nextString (); System. out. println ("user_name:" + userName);} else if (tagName. equals ("followers_count") {followsCount = jsReader. nextInt (); System. out. println ("followers_count:" + followsCount) ;}} jsReader. endObject ();}}

 

Output of the above content parsing:

11-22 06:59:52.441: I/System.out(5329): name:91234567890111-22 06:59:52.441: I/System.out(5329): text:How do I read JSON on Android?11-22 06:59:52.461: I/System.out(5329): skip======>11-22 06:59:52.461: I/System.out(5329): user_name:android_newb11-22 06:59:52.471: I/System.out(5329): followers_count:4111-22 06:59:52.481: I/System.out(5329): name:91234567890211-22 06:59:52.491: I/System.out(5329): text:@android_newb just use android.util.JsonReader!11-22 06:59:52.500: I/System.out(5329): 50.45472211-22 06:59:52.500: I/System.out(5329): -104.60666711-22 06:59:52.510: I/System.out(5329): user_name:jesse11-22 06:59:52.510: I/System.out(5329): followers_count:2

 

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.