I. Introduction to JSON:
JSON is constructed in two structures:
(1) A collection of "name/value" pairs (A collection of name/value pairs). In different languages, it is understood as objects (object), record (record), structure (struct), Dictionary (dictionary), hash table (hash table), keyed list (keyed list), or associative array (associative array).
(2) An ordered list of values (an ordered list of values). In most languages, it is understood as an array (array).
These are common data structures. In fact, most of the modern computer languages support them in some form. This makes it possible for a data format to be exchanged between programming languages that are also based on these constructs.
The JSON has these forms:
An object is an unordered collection of "name/value pairs". An object starts with "{" (opening parenthesis) and "}" (the closing parenthesis) ends. Each "name" is followed by a ":" (colon); "' Name/value ' pair ' is separated by", "(comma).
An array is an ordered collection of values (value). An array begins with "[" (the left square bracket), and "]" (the right square bracket) ends. Use "," (comma) to separate values.
The value(value) can be a string enclosed in double quotation marks (string), a numeric value (number), A,,, true object, false null or array. These structures can be nested.
A stringis a collection of any number of Unicode characters surrounded by double quotation marks, escaped with a backslash. A character (character) is a separate string (character string).
A stringis very similar to a C or Java string.
The value(number) is also very similar to the value of C or Java. Removes octal and hexadecimal formats that have not been used. Remove some coding details.
Two. Introduction to the JSON API in Android:
1.jsonobject,jsonarry,jsonstringer Class Build JSON file
2.getType can convert the value of the key you want to get to the specified type, if it cannot be converted, or if it is not worth throwing an exception jsonrexception
Opttype can also convert the value of the key you want to get to the specified type if it cannot be converted or if it is not worthwhile to return the value provided by the user or by default
Three. Use of JSON in Android:
Interface:
Mainactivity's main code:
Package Com.example.creatjson;import Java.io.bufferedreader;import Java.io.file;import java.io.FileOutputStream; Import Java.io.ioexception;import Java.io.inputstreamreader;import Java.io.unsupportedencodingexception;import Org.json.jsonarray;import Org.json.jsonexception;import Org.json.jsonobject;import Android.app.Activity;import Android.content.intent;import Android.os.bundle;import Android.view.view;import Android.view.View.OnClickListener Import Android.widget.button;public class Mainactivity extends Activity {private Jsonobject mcontactjson;private File File = null;private FileOutputStream FileOutputStream = null, @Overrideprotected void OnCreate (Bundle savedinstancestate {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); init ();} private void init () {button Btcreatjson = (button) Findviewbyid (R.id.bt_write_json); Btcreatjson.setonclicklistener ( New Onclicklistener () {@Overridepublic void OnClick (View v) {Intent Intent = new Intent (Mainactivity.this, Showjson. Class); Intent.putextra ("JSON", Creatjson ()); startactivity (intent);}}); Button Btreadjson = (button) Findviewbyid (R.id.bt_read_json); Btreadjson.setonclicklistener (new Onclicklistener () { String line; @Overridepublic void OnClick (View v) {try {inputstreamreader inputstreamreader = new InputStreamReader ( Getassets (). Open ("Json.json"), "UTF-8"); BufferedReader BufferedReader = new BufferedReader (InputStreamReader); StringBuilder StringBuilder = new StringBuilder (); while (line = Bufferedreader.readline ()) = null) { Stringbuilder.append (line);} Inputstreamreader.close (); Bufferedreader.close (); Jsonobject Mcontactjson = new Jsonobject (stringbuilder.tostring ()), line = Null;line + = "Stranger:" + Mcontactjson.get (" Stranger ") +" \ n "; Jsonarray Jsonarray = Mcontactjson.getjsonarray ("friend"); for (int i = 0; i < jsonarray.length (); i++) {Jsonobject JSON Object = Jsonarray.getjsonobject (i), line + = "Friendname:" + jsonobject.getstring ("friendname") + "number:" + jsonobject. GetString ("number") + "\ n";}} CAtch (Exception e) {e.printstacktrace ();} Intent Intent = new Intent (mainactivity.this, Showjson.class); Intent.putextra ("JSON", line); startactivity (Intent);});} Private String Creatjson () {try {Mcontactjson = new jsonobject (); Jsonarray mfriend = new Jsonarray (); Jsonobject friend1 = new Jsonobject () friend1.put ("Friendname", "Zhangsan"); Friend1.put ("Number", "131456789"); Jsonobject friend2 = new Jsonobject () friend2.put ("Friendname", "Lisi"); Friend2.put ("Number", "131456789"); Jsonobject Friend3 = new Jsonobject () friend3.put ("Friendname", "Liuwu"); Friend3.put ("Number", "131456789"); Mfriend.put (FRIEND1); Mfriend.put (Friend2); Mfriend.put (Friend3); Mcontactjson.put ("Friend", Mfriend); Mcontactjson.put ("Stranger", "134651313");} catch (Jsonexception e) {e.printstacktrace ();} return mcontactjson.tostring ();}}
Android_json Data Parsing demo:http://download.csdn.net/detail/two_water/9388361
Android_json Data parsing