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

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.