What is JSON
1. JSON (JavaScript Object Notation): a lightweight data exchange format based on the Javascript Language
2. JSON data is a collection of key-value pairs.
3. JSON has been accepted by most developers and is widely used in network data transmission.
4. JSON is easier to parse than XML
Features of JSON Data Format: JSON vs XML
Why use JSON?
For Ajax applications, JSON is easier to use than XML:
Differences from XML
- No end tag
- Shorter
- Faster read/write speed
- Ability to parse using the built-in Javascript eval () method
- Use Arrays
- Do not use reserved words
JSON data is written in the following format:Name/value pair.
Name/value pair includes the field name (in double quotation marks), followed by a colon, followed by a value:
Symbol explanation from top to bottom
[{"Name": "zhangsan", "Age": 20 },{ "name": "Lisi", "Age": 30}]
1. [starting to parse the Array
2. {start Object Parsing
3. "name": "zhangsan" resolves key-value pairs
4. "Age": 20 parse key-value pairs
5.} resolution object ends
6. {start Object Parsing
7. "name": "Lisi" resolves key-value pairs
8. "Age": 30. parse key-value pairs
9.} resolution object ends
10.] parsing array ends
Output result:
Above isOutput result, The following is the specific code implementation:
This Code explains how to useJsonreader
1 public class jsonutils {2 Public void parsejson (string jsondata) {3 try {4 // If JSON data needs to be parsed, first, you must generate a jsonreader object 5 jsonreader reader = new jsonreader (New stringreader (jsondata); 6 reader. beginarray (); // start to parse the array 7 while (reader. hasnext () {// hasnext is whether there is a next set of key-value pairs 8 string tagname = reader. nextname (); // reader. nextname () obtains the name 9 If (tagname. equals ("name") {10 system. out. println ("name --->" + reader. nextstring (); 11} 12 else if (tagname. equals ("Age") {// age is a number without quotation marks 13 system. out. println ("age --->" + reader. nextint (); // nextint obtains the integer 14} 15 16} 17 reader of the next key-value pair. endobject (); 18} 19 reader. endarray (); 20} 21 catch (exception e) {22 E. printstacktrace (); 23} 24} 25}
Public class mainactivity extends activity {// \ "Escape Character private string jsondata =" [{\ "Name \": \ "Michael \", \ "Age \": 20 }, {\ "Name \": \ "Mike \", \ "Age \": 21}] "; private button; Public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); button = (button) findviewbyid (R. id. buttonid); button. setonclicklistener (New buttonlistener ();} private class buttonlistener implements onclicklistener {public void onclick (view v) {jsonutils = new jsonutils (); jsonutils. parsejson (jsondata );}}}