Parsing JSON using Gson-1.6.jar

Source: Internet
Author: User
Tags tagname

Package Com.example.mars_2900_json01;import Java.io.stringreader;import Java.lang.reflect.type;import Java.util.iterator;import Java.util.linkedlist;import Com.google.gson.gson;import Com.google.gson.reflect.typetoken;import Com.google.gson.stream.jsonreader;public class JsonUtils {public static void Parsejson (String jsondata) {try {///If you need to parse JSON data, first generate a Jsonreader object//"[{\" name\ ": \" M Ichael\ ", \" age\ ": 20},{\" name\ ": \" mike\ ", \" age\ ": Up}]" Jsonreader reader = new Jsonreader (New StringReader (Jsond            ATA));                Reader.beginarray ();//Start parsing array while (Reader.hasnext ()) {reader.beginobject ();//Start parsing objects                    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.nextint ());            }} reader.endobject ();        } reader.endarray ();        } catch (Exception e) {e.printstacktrace ();        }} public static void Parseuserfromjson (String jsondata) {Gson Gson = new Gson ();        User user = Gson.fromjson (jsondata, User.class);        SYSTEM.OUT.PRINTLN ("Name--->" + user.getname ());    System.out.println ("Age---->" + user.getage ()); public static void Parseuserlistfromjson (String jsondata) {Type 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 ("Name--->" + user.getname ());        System.out.println ("Age---->" + user.getage ());   } }} 

Package Com.example.mars_2900_json01;import Android.support.v7.app.actionbaractivity;import Android.support.v7.app.actionbar;import Android.support.v4.app.fragment;import Android.os.Bundle;import Android.view.layoutinflater;import Android.view.menu;import Android.view.menuitem;import Android.view.View;import Android.view.viewgroup;import Android.view.view.onclicklistener;import Android.widget.button;import Android.os.build;public class Mainactivity extends Actionbaractivity {private String jsondata = "[{\" name\ ": \" michael\    ", \" age\ ": 20},{\" name\ ": \" mike\ ", \" age\ ": 21}]";    Private String jsonData2 = "{\" name\ ": \" json\ ", \" age\ ": 21}";    Private button button;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main);        Button = (button) Findviewbyid (R.id.buttonid);    Button.setonclicklistener (New Buttonlistener ()); } Private class Buttonlistener implements OnclicklistenER {@Override public void OnClick (View v) {Jsonutils.parsejson (jsondata);            Jsonutils.parseuserfromjson (JSONDATA2);                    Jsonutils.parseuserlistfromjson (Jsondata); }} @Override public boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu, this adds items to the        Action Bar if it is present.        Getmenuinflater (). Inflate (R.menu.main, menu);    return true; } @Override public boolean onoptionsitemselected (MenuItem Item) {//Handle Action Bar item clicks here.  The action bar would//automatically handle clicks on the Home/up button, so long/As you specify a parent        Activity in Androidmanifest.xml.        int id = item.getitemid ();        if (id = = r.id.action_settings) {return true;    } return super.onoptionsitemselected (item);     }/** * A placeholder fragment containing a simple view. */public static class PlaceholderfragmentExtends Fragment {public placeholderfragment () {} @Override public View Oncreateview (Layouti Nflater inflater, ViewGroup container, Bundle savedinstancestate) {View Rootview = Inflater.inf            Late (R.layout.fragment_main, container, false);        return rootview; }    }}

Parsing json with Gson-1.6.jar

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.