Parsing Android json data (jsonReader)
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) "," (comma) is used to separate the "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 useandroid.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 onAndroid?\"," + "\"geo\":null," +"\"user\":{\"name\":\"android_newb\",\"followers_count\":41}}," + "{\"id\": 912345678902," + "\"text\":\"@android_newb just useandroid.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. Then 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. end Array ();} 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