First, we introduce the definition of JSON, which is the abbreviation for JavaScript Object notation.
A lightweight data interchange format with good readability and fast-coding features. The industry's mainstream technology provides it with a complete solution (a bit like regular expressions that are supported by most languages today), allowing data exchange between platforms. JSON uses a highly compatible text format, as well as behavior similar to the C language system.
The structure of JSON:
(1) Name/value pairs (unordered): Similar to the well-known keyed list, Hash table, disctionary and associative array. There is another class "Bundle" in the Android platform that has similar behavior in some ways.
(2) Array (ordered): A set of ordered list of data.
One: The nature of JSON and its position in data interaction are needless to say, look directly at the case.
First create the assets file directory in Android studio to hold the JSON data file, and there is no assets folder in the default project file directory of the Android Studio 1.3.
So we need to create and create the following methods:
After you create a assets file directory, create a Text.json file in its directory.
Second: How to obtain the JSON data in the assets file directory:
Under Eclipse are: InputStreamReader (Getassets (). Open ("Text.json"), "UTF-8"), obtain the file data, and return the data in InputStream.
In Android Studio, it is through: JsonLearn.this.getClass (). getClassLoader (). getResourceAsStream ("assets/" + "Text.json"); Returns the corresponding InputStream.
Three: Case show:
1: Case Project App interface as follows, through the button to achieve JSON data reading and creation, and display in the TextView.
2: The code is as follows:
Package Activity.cyq.datalrearn;
Import android.support.v.app.appcompatactivity;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.widget.TextView;
Import Org.json.JSONArray;
Import org.json.JSONException;
Import Org.json.JSONObject;
Import Java.io.BufferedReader;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.InputStreamReader;
public class Jsonlearn extends Appcompatactivity {private TextView writetext, ReadText;
@Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_json_learn);
ReadText = (TextView) Findviewbyid (R.id.readjsontext);
WRITETEXT = (TextView) Findviewbyid (R.id.writejsontext); /* Read JSON data */Findviewbyid (R.ID.READJSIOBTN). Setonclicklistener (New View.onclicklistener () {@Override P ublic void OnClick (View v) {/* Gets the data for the Text.json file under the assets file and returns in the form of an output stream. */InputStream is = jsonlEarn.this.getClass (). getClassLoader (). getResourceAsStream ("assets/" + "Text.json");
InputStreamReader StreamReader = new InputStreamReader (IS);
BufferedReader reader = new BufferedReader (StreamReader);
String Line;
StringBuilder StringBuilder = new StringBuilder ();
try {while (line = Reader.readline ())!= null) {//Stringbuilder.append (line);
Stringbuilder.append (line);
} reader.close ();
Reader.close ();
Is.close ();
catch (IOException e) {e.printstacktrace ();
try {jsonobject person = new Jsonobject (stringbuilder.tostring ());
Jsonarray Infarray = Person.getjsonarray ("INF");
for (int i =; I < infarray.length (); i++) {Jsonobject Inf_array = Infarray.getjsonobject (i);
Readtext.append ("Name:" + inf_array.getstring ("name") + "\ n"); Readtext.append("Idcard:" + inf_array.getstring ("Idcard"));
Readtext.append ("Age:" + inf_array.getint ("age"));
Readtext.append ("Married:" + Inf_array.getboolean ("Married"));
} catch (Jsonexception e) {e.printstacktrace ();
}
}
});
/* Create JSON data and display/Findviewbyid (R.ID.WRITEJSIOBTN). Setonclicklistener (New View.onclicklistener () {@Override
public void OnClick (View v) {try {jsonobject inf = new Jsonobject ();
Inf.put ("number",);
Jsonarray array = new Jsonarray ();
Jsonobject arr_ = new Jsonobject ();
Arr_.put ("name", "John");
Arr_.put ("Age",);
Arr_.put ("Idcard", "XC");
Arr_.put ("Married", true);
Jsonobject arr_ = new Jsonobject ();
Arr_.put ("name", "Dick");
Arr_.put ("Age",);
Arr_.put ("Idcard", "@DC");
Arr_.put ("Married", true);
Array.put (, arr_); Array.put (, arr_);
Inf.put ("INF", array);
Writetext.settext (Inf.tostring ());
catch (Jsonexception e) {e.printstacktrace ();
}
}
}); }
}
The above is the way to read and create JSON data from Android, hopefully it will help you, in real project development, through the two JSON processing APIs of Gson (Google) Fast-json (Alibaba).