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.

below is android The official set of Json

[{"id": 912345678901, "text": "How does I read JSON on Android?", "geo": null, "user": {"name": "Android Oid_newb "," Followers_count ":", "{" id ": 912345678902," text ":" @android_newb just Useandroid.util . jsonreader! "," Geo ": [50.454722, -104.606667]," user ": {" name ":" Jesse "," Followers_count ": 2}}]

Json Span style= "font-family: ' The song Body '; > " java code, create a json data," symbols need to be escaped)

private string jsondate =  "["             +  "{\" id\ ":  912345678901,"            +   "\" text\ ": \" how do i read json onandroid?\ ","              +  "\" geo\ ": null,"             + "\" User\ ": {\" name\ ": \" android_newb\ ", \" followers_count\ ": ()}},"             +  "{\" id\ ":  912345678902,"             +  "\" text\ ": \" @android_newb  just  Useandroid.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.

 

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{            //Creating Jsonreader Objects             jsonreader jsreader = new jsonreader (new  StringReader (jsondate));            jsreader.beginarray ( );           while  (JsReader.hasNext ())  {                readmessage (JsReader );           }            jsreAder.endarray ();       }           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"))  {       &nbsP;            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 value                     jsreader.skipvalue () ;                    system.out.println ("skip======>");                 }           }            jsreader.endobject ();        }       //parsing data in Geo         Public void readdoublearray (Jsonreader jsreader)  throws IOException{            jsreader.beginarray ();            while (Jsreader.hasnext ()) {             &nbsP;  system.out.println (Jsreader.nextdouble ());            }           jsreader.endarray ();        }       //due to reading data from 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"))  {       &Nbsp;         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:

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

above! In addition to the online full range of security, compatibility test for the APP, I will use this:www.ineice.com.


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.