<span id="Label3"></p><p><p>Method One: parse the JSON data by getting the Jsonreader object:</p></p><pre class="brush:java;toolbar:false"> String jsonData = "[{\" username\ ": \" arthinking\ ", \" userid\ ": 001},{\" username\ ": \" jason\ ", \" userid\ ": 002}]"; try{ jsonreader reader = new jsonreader (new stringreader (jsonData)); reader.beginarray (); while (reader.hasnext ()) { reader.beginobject (); while (reader.hasnext ()) { string tagname = reader.nextname (); if (tagname.equals ("username")) { &nbsP; system.out.println ( Reader.nextstring ()); } else if (tagname.equals ("userId")) { system.out.println (reader.nextString () ); } } reader.endobject (); } reader.endarray (); } catch (Exception e) {&NBSP;&NBSP;&NBSP;&NBSP;&NBSp; e.printstacktrace (); } </pre><p><p>Method Two: by mapping the JSON data into an object, use the Fromjson () method of the Gson object to get an array of objects to manipulate:</p></p><p><p>Create a Pojo object corresponding to the JSON data user.java:</p></p><pre class="brush:java;toolbar:false"><pre class="brush:java;toolbar:false">public class user { private String username ; private int userId ; public string getusername () { return username; } public void Setusername (string username) { this.username = username; } public int GetUserId () { return userId; } public void setuserid (int userid) { this.userId = userId; } }</pre></pre><p><p>Use the Gson object to get the user object data for the appropriate operation:</p></p><pre class="brush:java;toolbar:false"><pre class="brush:java;toolbar:false">ype ListType = new typetoken<linkedlist<user>> () {}.gettype (); Gson Gson = new Gson (); linkedlist<user> users = Gson.fromjson (jsondata, listtype); For (Iterator Iterator = users.iterator (); iterator.hasnext ();) {user user = (user) Iterator.next (); System.out.println (user.getusername ()); System.out.println (user.getuserid ()); }</pre></pre><p><p>If the JSON string you are working with contains only one JSON object, you can use Fromjson to get a user object directly:</p></p><pre class="brush:java;toolbar:false"><pre class="brush:java;toolbar:false">Tring Jsondata = "{\" username\ ": \" arthinking\ ", \" userid\ ": 001}"; Gson Gson = new Gson (); User user = Gson.fromjson (jsondata, user.class); System.out.println (user.getusername ()); System.out.println (user.getuserid ());</pre></pre><p><p><br></p></p><p><p>Two ways to parse JSON data using Gson in Android</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