Android JSON data parsing and encapsulation small demo

Source: Internet
Author: User

Today's early work, wrote a simple JSON Demo, summed up here.

The first is:


For simplicity, the parsing part does not adopt the method of parsing the URL, directly write the JSON into the string, logic is relatively simple, here is not verbose, on the code:

Package Com.example.jsondemo;import Org.json.jsonarray;import Org.json.jsonexception;import org.json.JSONObject; Import Android.app.activity;import android.os.bundle;import Android.view.menu;import Android.view.MenuItem;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.button;import Android.widget.edittext;import Android.widget.textview;import Android.widget.toast;public class Main extends Activity {//private static final String base_url =//"http://zhoumushui.sinaapp.com/json/";p rivate TextView tvmsg; Private TextView tvjson;private EditText etname;private EditText etage;private string strjson = "";p rivate string Staffinf o = "";p rivate string strjsonres = "";p rivate string strmsg;private button Btnadd;private button btnjson; @Overridepublic V OID onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.main); TVMSG = ( TextView) Findviewbyid (r.id.tvmsg); Tvjson = (TextView) Findviewbyid (R.id.tvjson); etname = (EdittEXT) Findviewbyid (r.id.etname), EtAge = (EditText) Findviewbyid (r.id.etage); btnadd = (Button) Findviewbyid (r.id.btnadd ); Btnjson = (Button) Findviewbyid (R.id.btnjson); Msgtojson (); Package jsonjsontomsg (); Parsing Json}class Onclicklistenerimp implements Onclicklistener {@Overridepublic void OnClick (View v) {//TODO Auto-generat Ed method stubif (v = = Btnadd) {if (STRMSG! = null && Strjson.trim (). Length ()! = 0) {STRMSG = StrMsg + ", {name: '" + Etname.gettext (). ToString () + "', Age:" + Etage.gettext (). ToString () + "}";} else {strMsg = "{staff:[{name: '" + etname.gettext (). ToString () + "', Age:" + Etage.gettext (). ToString () + "}";} Toast.maketext (Main.this, "Add succcess!", Toast.length_short). Show (); Etage.settext (""); Etname.settext ("");} else if (v = = Btnjson) {strjsonres = ""; strjsonres = StrMsg + "]}"; Tvjson.settext (Strjsonres);}} private void Msgtojson () {Btnadd.setonclicklistener (New Onclicklistenerimp ()); Btnjson.setonclicklistener (new Onclicklistenerimp ());} private void Jsontomsg () {Strjson = "{staff:[{name: ' Alex ', age:21},{name: ' Zhou ', age:22},{name: ' Anne ', Age:23}],company: ' T-chip '}"; staffinfo = "raw data: \ N "+ Strjson +" \ n \ nthe parse: \ n "; try {jsonobject mjsonobject = new Jsonobject (Strjson); Jsonarray Mjsonarray = Mjsonobject.getjsonarray ("Staff"); String company = mjsonobject.getstring, Staffinfo = staffinfo + "Total" + mjsonarray.length () + "Employees, information such as Under: \ n "; for (int staffcount = 0; Staffcount < Mjsonarray.length (); staffcount++) {//Get employee jsonobject staff = mjsonarray.ge Tjsonobject (staffcount); int staffno = Staffcount + 1;staffinfo = staffinfo + "ordinal:" + staffno + "Name:" + staff.getstring (" Name ") +" Age: "+ Staff.getint (" ages ") +" \ n ";} Tvmsg.settext (staffinfo);} catch (Jsonexception e) {e.printstacktrace ();} catch (Exception e) {e.printstacktrace ()}}}


The layout is a bit dilatory, in fact the Data Encapsulation section can also take advantage of the logic of the parsing part.

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layout_width=" Fill_parent "android:layout_height=" fill_parent "android:orientation=" vertical "&G    T <textview android:id= "@+id/tvmsg" android:layout_width= "fill_parent" android:layout_height= "Wrap_c        Ontent "/> <textview android:layout_width=" fill_parent "android:layout_height=" Wrap_content " android:text= "=========================="/> <linearlayout android:layout_width= "Fill_parent" Android oid:layout_height= "wrap_content" android:orientation= "horizontal" > <edittext android:id= "@ +id/etname "android:layout_width=" wrap_content "android:layout_height=" Wrap_content "Android            oid:hint= "name"/> <edittext android:id= "@+id/etage" android:layout_width= "Wrap_content" Android:layout_height= "Wrap_content" android:hint= "Age"/> <button android:id= "@+id/btnadd" and          Roid:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:text= "Add"/> <button android:id= "@+id/btnjson" android:layout_width= "Wrap_content" android:layou t_height= "Wrap_content" android:text= "JSON"/> </LinearLayout> <textview Andro Id:id= "@+id/tvjson" android:layout_width= "fill_parent" android:layout_height= "Wrap_content"/></Linear Layout>


Reprint Please specify source: Zhou Mushi's csdn blog Http://blog.csdn.net/zhoumushui

My github: Zhou Mushi's GitHub Https://github.com/zhoumushui


Android JSON data parsing and encapsulation small demo

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.