Small json parsing example for android

Source: Internet
Author: User

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"
}
]
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.