What is JSON:
JSON is a lightweight text data interchange Format
JSON independent of language and platform
JSON is self-descriptive and easier to understand
JSON syntax:
Data in name/value pairs
Data separated by commas
Curly braces represent objects
The brackets represent the array
JSON uses:
Mainactivity.java
Packagecn.lixyz.jsontest.activity;ImportJava.io.BufferedReader;ImportJava.io.File;ImportJava.io.FileWriter;Importjava.io.IOException;ImportJava.io.InputStreamReader;Importjava.io.UnsupportedEncodingException;ImportOrg.json.JSONArray;Importorg.json.JSONException;ImportOrg.json.JSONObject;Importandroid.app.Activity;ImportAndroid.os.Bundle;Importandroid.os.Environment;ImportAndroid.util.Log;ImportAndroid.view.View;ImportCN.LIXYZ.JSONTEST.R; Public classMainactivityextendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); } Public voidClickbutton (View v) {Switch(V.getid ()) { CaseR.id.bt_readjson:readjson (); Break; CaseR.id.bt_writejson:writejson (); Break; } } //writing files to the SDcard Private voidWritejson () {//Create a JSON objectJsonobject root =NewJsonobject (); Try { //inserting elements using put (Kay,value)Root.put ("Class", "Second class three years"); //several common objects used to join an arrayJsonobject Student1 =NewJsonobject (); Student1.put ("id", 1); Student1.put ("Name", "Zhang San"); Student1.put ("Age", 10); Jsonobject Student2=NewJsonobject (); Student2.put ("id", 2); Student2.put ("Name", "John Doe"); Student2.put ("Age", 11); Jsonobject Student3=NewJsonobject (); Student3.put ("ID", 3); Student3.put ("Name", "Harry"); Student3.put ("Age", 13); Jsonobject Student4=NewJsonobject (); Student4.put ("ID", 4); Student4.put ("Name", "Zhao Liu"); Student4.put ("Age", 14); Jsonobject student5=NewJsonobject (); Student5.put ("ID", 5); Student5.put ("Name", "Sun Seven"); Student5.put ("Age", 15); Jsonobject Student6=NewJsonobject (); Student6.put ("id", 6); Student6.put ("Name", "Liu Ba"); Student6.put ("Age", 16); //Create a JSON arrayJsonarray array =NewJsonarray (); //Add the JSON object you created earlierArray.put (STUDENT1); Array.put (Student2); Array.put (STUDENT3); Array.put (STUDENT4); Array.put (STUDENT5); Array.put (STUDENT6); //add a JSON arrayRoot.put ("Student", array); LOG.D ("Tttt", array.tostring ()); } Catch(jsonexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } String PackageName= This. Getpackagename (); String Jsonfilename= "Json.json"; String Sdcardpath=environment.getexternalstoragedirectory (). toString (); File Sdcardpackagepath=NewFile (Sdcardpath + "/" +PackageName); if(!sdcardpackagepath.exists ()) { if(Sdcardpackagepath.mkdir ()) {LOG.D ("Tttt", "Create directory Success"); } Else{LOG.D ("Tttt", "Create Directory not successful"); }} File Jsonfile=NewFile (Sdcardpackagepath + "/" +jsonfilename); if(!jsonfile.exists ()) { Try { if(Jsonfile.createnewfile ()) {LOG.D ("Tttt", "Create File succeeded"); } Else{LOG.D ("Tttt", "Failed to create file"); } } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); } } Try { //writing JSON data to a fileFileWriter FileWriter =NewFileWriter (Jsonfile); Filewriter.write (Root.tostring ()); Filewriter.flush (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); } } //reading JSON files in the assets directory Private voidReadjson () {Try { //read the JSON file in the assets directoryInputStreamReader ISR =NewInputStreamReader (Getassets (). Open ("Test.json"), "UTF-8"); BufferedReader BR=NewBufferedReader (ISR); String Line; StringBuilder Builder=NewStringBuilder (); while(line = Br.readline ())! =NULL) {builder.append (line); } br.close (); //get to the object in the JSON fileJsonobject root =NewJsonobject (builder.tostring ()); //Get value using GetString (key)LOG.D ("Tttt", "class=" + root.getstring ("class"))); //get JSON arrayJsonarray array = root.getjsonarray ("Student"); for(inti = 0; I < Array.Length (); i++) {Jsonobject student=Array.getjsonobject (i); LOG.D ("Tttt", "id=" + student.getint ("id") + ", name=" + student.getstring ("name") + ", age=" + student.get Int ("Age")); } } Catch(unsupportedencodingexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(jsonexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } }}
Activity_main.xml
<LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical"Android:paddingbottom= "@dimen/activity_vertical_margin"Android:paddingleft= "@dimen/activity_horizontal_margin"Android:paddingright= "@dimen/activity_horizontal_margin"Android:paddingtop= "@dimen/activity_vertical_margin"Tools:context= "Cn.lixyz.jsontest.activity.MainActivity" > <ButtonAndroid:id= "@+id/bt_writejson"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:onclick= "Clickbutton"Android:text= "@string/writejson" /> <ButtonAndroid:id= "@+id/bt_readjson"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:onclick= "Clickbutton"Android:text= "@string/readjson" /></LinearLayout>
/assets/test.json
{" student": [ {"id": 1, "name": "Zhang San", "Age": ten}, {"id": 2, "name": "John Doe", "Age": one}, {"id": 3, "name": " Harry "," Age ": 4," name ":" Zhao Liu "," Age ": seven}, {" id ": 5," name ":" Sun "," Age ": +}, {" id ": 6," name ":" Liu Ba "," Age ": [}, ], " class ":" Three-year second class " }
Android Notes (50) JSON data in Android