Parsing JSON-formatted data on Android

Source: Internet
Author: User

Package Com.practice.json; Import Org.json.jsonarray;import Org.json.jsonexception;import org.json.JSONObject; Import Android.app.activity;import Android.os.bundle;import Android.util.Log;     public class Jsondemo extends Activity {/*http://www.huiyi8.com/vi/* Parse JSON example, str is stored in JSON code, the parsed data in the LOGCAT output */String TAG = "Json message";@Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.main);    Detectjson (); } private void Detectjson () {String str = "{" +"\" date \ ": \" 2011-06-06\ "," +//like is Jsonobject ."\" like\ ": {" +/vi Design"\" name\ ": \" Garnett \ "," +"\" height\ ": \" 2.11cm\ "," +"\" age\ ":" +   "},"+//likelist is a jsonobject ."\" likelist\ ":" +"{\" list\ ":" +   "["+ //This is also jsonobject   "{"+"\" name\ ": \" rose\ "," +"\" height\ ": \" 190cm\ "," +"\" age\ ":" +   "},"+//This is also jsonobject   "{"+"\" name\ ": \" kobe \ "," +"\" height\ ": \" 198cm\ "," +"\" age\ ":" +   "}"+   "]"+       "}"+       "}";try {jsonobject Datajson = new Jsonobject (str);log.d (TAG, datajson.getstring ("date"));Jsonobject Nbajson = Datajson.getjsonobject ("like");log.d (TAG, nbajson.getstring ("Name"));log.d (TAG, nbajson.getstring ("Height"));log.d (TAG, Nbajson.get ("Age"). ToString ());Jsonobject Listjson = Datajson.getjsonobject ("Likelist");Jsonarray Arrayjson = Listjson.getjsonarray ("List");for (int i=0;i<arrayjson.length (); i++) {Jsonobject Tempjson = Arrayjson.optjsonobject (i);log.d (TAG, tempjson.getstring ("Name"));log.d (TAG, tempjson.getstring ("Height"));log.d (TAG, tempjson.getstring ("Age"). ToString ()); } } catch (Jsonexception e) {System.out.println ("Something wrong ...");e.printstacktrace (); }}}

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.