Parsing of the JSON data for Android (Jsonreader)

Source: Internet
Author: User
Tags tagname

The parsing of JSON data is relatively easy, and the code implemented is quite simple. The Jsonreade method is used to parse the JSON data.

1. Before parsing, you need to know what JSON data is.

The JSON data store object is a collection of unordered name/value pairs. Compared with other data storage methods, the readability, extensibility, coding difficulty and decoding difficulty of JSON data have certain advantages. In the JSON data,

For an object:

(1) An object starts with "{" (opening parenthesis) and "}" (closing parenthesis) ends.

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

(3) "' Name/value ' pair" is separated by "," (comma).

For an array:

(1) An array begins with "[" (the left square bracket), and "]" (the right square bracket) ends.

(2) Use "," (comma) to separate values.

Here is an example of a set of JSON data that is officially given by Android:

[   {     "id": 912345678901,     "text": "How does I read JSON on Android?") ,      NULL ,     " User ": {       " name ":" Android_newb ",       " Followers_count ": ()}     },   {      "id": 912345678902,     "text": "@android_newb just use android.util.jsonreader!" ,     " Geo ": [50.454722, -104.606667],     " user ": {       " name ":" Jesse ",       " Followers_count ": 2     }   }]

In code, if you define JSON data directly, you need to "use \ Escape" in your code. The above JSON is in the code in the form of: (in Java code, create a piece of JSON data, "symbols need to be escaped)

    Private String jsondate = "["            + "{\" id\ ": 912345678901,"            + "\" text\ ": \" How does I read JSON on android?\ ","            + "\" geo\ ": N ull, "            +" \ "User\": {\ "name\": \ "android_newb\", \ "followers_count\": "+"}, "            +" {\ "id\": 912345678902, "            +" \ "text\": \ "@android_newb just use android.util.jsonreader!\", "            +" \ "geo\": [50.454722,-104.606667], "            +" \ " User\ ": {\" name\ ": \" jesse\ ", \" followers_count\ ": 2}}"            + "]";

1. Using the Jsonreader method to parse the JSON data object, you need to create a Jsonreader object.

2. Then use Beginarray () to begin parsing [the first array on the left.

3. Then use BeginObject () to begin parsing the first object in the array {.

4. For direct data can be directly resolved to the data, but for nested in the JSON array of data, you need to write a parsing method.

5. After parsing is complete, do not forget to use Endarray (), EndObject () to turn off parsing.

 Packagecom.mecury.jsontest;Importjava.io.IOException;ImportJava.io.StringReader;ImportAndroid.util.JsonReader;ImportAndroid.util.JsonToken; Public classJsonutils { Public voidParsejson (String jsondate)throwsioexception{//Create a Jsonreader objectJsonreader Jsreader =NewJsonreader (NewStringReader (jsondate));            Jsreader.beginarray ();  while(Jsreader.hasnext ()) {readmessage (Jsreader);        } jsreader.endarray (); }             Public voidReadmessage (Jsonreader Jsreader)throwsioexception{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 Current ValueJsreader.skipvalue (); System.out.println ("Skip======>");        }} jsreader.endobject (); }        //parsing data in Geo         Public voidReaddoublearray (Jsonreader Jsreader)throwsioexception{Jsreader.beginarray ();  while(Jsreader.hasnext ()) {System.out.println (jsreader.nextdouble ());        } jsreader.endarray (); }        //because the data in user is read         Public voidReaduser (Jsonreader Jsreader)throwsioexception{String userName=NULL; intFollowscount =-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 to parse the above content:

 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

Parsing of the JSON data for Android (Jsonreader)

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.