Android parses JSON

Source: Internet
Author: User

The android framework has integrated JSON parsing packages for us.
First, a simple example is provided. JSON is directly written in string.
Java Code
String staticobject = "{\" firstname \ ": \" Steve \ ", \" lastname \ ": \" Jobs \ ", \" cellphones \": \ "0 \"}";
Void buildobject ()
{
Try
{
OBJ = new jsonobject (staticobject );
String x = obj. get ("firstname "). tostring () + "" + obj. get ("lastname "). tostring () + "has" + obj. getint ("cellphones") + "Android phones. ";
Setstatus (X );
}
Catch (jsonexception je)
{
Setstatus ("error occured" + je. getmessage ());
}
}
Void setstatus (string X)
{
Textview TV = (textview) findviewbyid(r.id.txt status );
TV. settext (X );
}
String staticobject = "{\" firstname \ ": \" Steve \ ", \" lastname \ ": \" Jobs \ ", \" cellphones \": \ "0 \"} "; void buildobject () {try {OBJ = new jsonobject (staticobject); string x = obj. get ("firstname "). tostring () + "" + obj. get ("lastname "). tostring () + "has" + obj. getint ("cellphones") + "Android phones. "; setstatus (x);} catch (jsonexception je) {setstatus (" error occured "+ je. getmessage () ;}} void setstatus (string X) {textview TV = (textview) findviewbyid(r.id.txt status); TV. settext (x );}
Write JSON in the file and put it in the raw directory.
JSON format:
Java code
{
"Firstname": "Richard ",
"Lastname": "Stearns ",
"Almamater": "Cornell University ",
"Occupation": "President, World Vision ",
"Interview ":
{
"Source": "http://blog.guykawasaki.com/2007/05/ten_or_so_quest.html#ixzz0giEIX0zY ",
"Questions ":
[
{
"Question": "How much money does World Vision raise every year? ",
"Answer": "worldwide, World Vision raises about $2 billion annually; the u. S. Office, which I head up, raises about half of the total ."
},
{
"Question": "Is this the 80/20 rule where twenty percent of the people send in eighty percent of the money or are donations more spread out? ",
"Answer": "World Vision's strength is that we are supported by hundreds of thousands of faithful people who give us about a dollar a day by sort soring children. our \ "major donors \" account for less than five percent of our total income. also, for a non-profit, we have quite a diversified portfolio of revenue. just over forty percent is cash from private citizens; thirty percent is government grants in food and cash; and about thirty percent are products donated from corporation -- what we call \ "gifts-in-kind. \""
}
]
}
}
{"Firstname": "Richard", "lastname": "Stearns", "almamater": "Cornell University", "Occupation": "President, World Vision ", "Interview": {"Source": "http://blog.guykawasaki.com/2007/05/ten_or_so_quest.html#ixzz0giEIX0zY", "Questions": [{"Question": "How much money does World Vision raise every year? "," Answer ":" worldwide, World Vision raises about $2 billion annually; the U. s. office, which I head up, raises about half of the total. "}, {" Question ":" Is this the 80/20 rule where twenty percent of the people send in eighty percent of the money or are donations more spread out? "," Answer ":" World Vision's strength is that we are supported by hundreds of thousands of faithful people who give us about a dollar a day by sort soring children. our \ "major donors \" account for less than five percent of our total income. also, for a non-profit, we have quite a diversified portfolio of revenue. just over forty percent is cash from private citizens; thirty percent is government grants in food and cash; and about thirty percent are products donated from corporation -- what we call \ "gifts-in-kind. \ ""}]}
The parsing code is
Java code
Void buildobjectfromfile ()
{
Try
{
String x = "";
Inputstream is = This. getresources (). openrawresource (R. Raw. Interview );
Byte [] buffer = new byte [is. Available ()];
While (is. Read (buffer )! =-1 );
String JSON = new string (buffer );
OBJ = new jsonobject (JSON );
X = obj. getstring ("firstname") + "" + obj. getstring ("lastname") + "N ";
X + = obj. getstring ("Occupation") + "N ";
Jsonobject interview = obj. getjsonobject ("interview ");
X + = "Interview Source:" + interview. getstring ("Source") + "N ";
Jsonarray questions = interview. getjsonarray ("Questions ");
X + = "number of questions:" + questions. Length () + "Nn ";
Int I;
For (I = 0; I <questions. Length (); I ++)
{
Jsonobject QA = questions. getjsonobject (I );
X + = "------------ N ";
X + = "Q" + (I + 1) + "." + qa. getstring ("Question") + "Nn ";
X + = "A" + (I + 1) + "." + qa. getstring ("Answer") + "N ";
}
Setstatus (X );
}
Catch (exception je)
{
Setstatus ("error w/file:" + je. getmessage ());
}
}

Related Article

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.