<span id="Label3"></p><p><p>JSON is a common Xml-like data Interchange format that has a higher transfer efficiency than xml.<br>structurally, all data can eventually be decomposed into three types:</p></p><p><p>The first type is scalar (scalar), which is a single string (string) or number (numbers), such as "beijing", a separate Word.<br>The second type is the sequence (sequence), which is a number of related data that are tied together in a certain order, also called an array or list, such as "beijing, shanghai."<br>The third type is mapping (mapping), a name/value pair (name/value), which has a name and a value corresponding to it, also known as Hash (hash) or dictionary (dictionary), such as "capital: beijing".<br>JSON specifications are very simple, with only one page hundreds of words can be said clearly, and Douglas Crockford claims that this specification never need to upgrade, because the provisions of the Provision.<br>1) the data is separated by commas (",").<br>2) the map is represented by a colon (":").<br>3) a set (array) of side data is expressed in square brackets ("[]").<br>4) the set of mappings (objects) are represented by curly braces ("{}").<br><br></p></p><p><p>You can use Gson to parse JSON data in ANDROID.<br>first, Download Gsonapi:google-gson2.2.4.rar from code.google.com/p/google-gson/downloads/list<br>Copy the Gson-2.2.4.jar to Libs (the Project root directory creates a new libs folder).<br>You can use the following two methods to parse JSON Data:<br><strong>1. Parse the JSON data by getting the Jsonreader object:</strong><br>Mainactivity.java:<br></p></p><pre name="code" class="java"><pre name="code" class="java">Package Ycw.json01;import Android.app.activity;import Android.os.bundle;import android.view.view;import Android.view.view.onclicklistener;import Android.widget.button;public class Mainactivity extends Activity implements Onclicklistener {private String jsondata = "[{\" name\ ": \" yen\ ", \" age\ ": 22},{\" name\ ": \" lee\ ", \" age\ ": $}]";p rivate Button btn; @Overrideprotected void onCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); btn = (Button) Findviewbyid (r.id.btn01); btn.setonclicklistener (this);} @Overridepublic void OnClick (View v) {if (v.getid () = = r.id.btn01) {jsonutils ju = new jsonutils (); Ju.parsejson (jsondata); }}}</pre></pre>Jsonutils.java:<br><pre name="code" class="java"><pre name="code" class="java">Package Ycw.json01;import Java.io.stringreader;//import Android.util.jsonreader;import Com.google.gson.stream.jsonreader;public class jsonutils {public void Parsejson (String jsondata) {try {///if you need to parse JSON data, The first thing to generate is a Jsonreader object Jsonreader reader = new Jsonreader (new stringreader (jsondata)); reader.beginarray (); while ( Reader.hasnext ()) {reader.beginobject (); while (reader.hasnext ()) {String tagName = Reader.nextname (); if ( Tagname.equals ("name")) {System.out.println ("name--->" + reader.nextstring ()),} else if (tagname.equals ("age")) { System.out.println ("age--->" + reader.nextstring ());}} Reader.endobject ();} Reader.endarray ();} Catch (Exception e) {e.printstacktrace ();}}}</pre></pre><strong><strong>2, by mapping the JSON data into an object, use the Fromjson () method of the Gson object to get an array of objects to manipulate:</strong></strong><br>Create a Pojo object corresponding to the JSON data usermodel.java:<br><pre name="code" class="java"><pre name="code" class="java">Package Ycw.json02;public class Usermodel {private string Name;private string Age;public string getName () {return name;} public void SetName (String Name) {this.name = name;} Public String getage () {return age;} public void Setage (String Age) {this.age = age;}}</pre></pre>Use the Gson object to get the user object data for the appropriate operation:<br>Mainactivity.java:<br><pre name="code" class="java"><pre name="code" class="java">Package Ycw.json02;import Android.app.activity;import Android.os.bundle;import android.view.view;import Android.view.view.onclicklistener;import Android.widget.button;public class Mainactivity extends Activity implements Onclicklistener {private String jsondata = "[{\" name\ ": \" yen\ ", \" age\ ": 22},{\" name\ ": \" lee\ ", \" age\ ": $}]";p rivate String jsondata2= "{\" name\ ": \" yen\ ", \" age\ ":";p rivate button btn; @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); setcontentview (r.layout.activity_main); btn = (Button) Findviewbyid (r.id.btn02); Btn.setonclicklistener (this);} @Overridepublic void OnClick (View v) {if (v.getid () = = r.id.btn02) {jsonutils ju = new jsonutils (); Ju.parseuserfromjson (js onData); Ju.parseuserfromjson2 (jsonData2);}}</pre></pre>Jsonutils.java:<br><pre name="code" class="java"><pre name="code" class="java">package ycw.json02;import java.lang.reflect.type;import java.util.iterator;import Java.util.linkedlist;import Com.google.gson.gson;import Com.google.gson.reflect.typetoken;public class JsonUtils { public void Parseuserfromjson (String jsondata) {Type listtype = new typetoken<linkedlist<usermodel>> () {}. GetType (); Gson Gson = new Gson (); linkedlist<usermodel> users = Gson.fromjson (jsondata, listtype); for (Iterator Iterator = Users.iterator (); Iterator.hasnext ();) {usermodel user = (usermodel) Iterator.next (); System.out.println ("name-->" + user.getname ()); System.out.println ("age->" + user.getage ());}} The JSON string that is processed contains only one JSON object, you can use Fromjson to get a user object public void ParseUserFromJson2 (String jsondata) {gson gson = new Gson (); Usermodel user = Gson.fromjson (jsondata, usermodel.class); System.out.println ("name->" + user.getname ()); System.out.println ("age->" + user.getage ());}} </pre></pre><p><p>more--->http://blog.csdn.net/linjiaxingqqqq/article/details/7238235</p></p><p><p>Welcome to AC http://blog.csdn.net/ycwol/article/details/46318831</p></p><p><p>ANDROID_ parsing JSON data using Gson</p></p></span>
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