I learned how to parse json. I will share with you a small example.
The following code is used:
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
String x;
JSONObject obj;
Try {
InputStream is = this. getResources (). openRawResource (R. raw. json );
Byte [] buffer = new byte [is. available ()];
Is. read (buffer );
TextView v = new TextView (this );
String json = new String (buffer, "UTF-8 ");
Obj = new JSONObject (json );
X = obj. getString ("name ");
Log. d ("======= name =========", x );
X = obj. getString ("url ");
Log. d ("======= URL ========", x );
X = obj. getString ("abstract ");
Log. d ("======= abstract ========", x );
JSONObject obj1 = obj. getJSONObject ("url data ");
X = obj1.getString ("Comprehensive ");
Log. d ("====================", x );
X = obj1.getString ("level ");
Log. d ("====================", x );
X = obj1.getString ("quantity ");
Log. d ("========= quantity ========", x );
JSONArray array = obj1.getJSONArray ("Comprehensive ");
Obj = array. getJSONObject (0 );
X = obj. getString ("Comprehensive 1 ");
Log. d ("======== comprehensive 1 ========", x );
Obj = array. getJSONObject (1 );
X = obj. getString ("Comprehensive 2 ");
Log. d ("======= comprehensive 2 ========", x );
JSONStringer s = new JSONStringer ();
Log. d ("=================================", s. object (). key (""). value ("aaa "). endObject (). toString ());
} Catch (Exception e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
Json file:
{
"Name": "400 phone ",
"Url": "http://www.my400800.cn ",
"Abstract ":"",
"Website data ":
{
"Quantity": "60 ",
"Level": "61 ",
"Unit": "62 ",
"Comprehensive ":
[
{
"Comprehensive 1": "100"
},
{
"Comprehensive 2": "110"
}
]
}
}