Analysis of methods for analyzing JSON data by Android _android

Source: Internet
Author: User
Tags tagname

The example in this article describes how Android parses JSON data. Share to everyone for your reference, specific as follows:

As a "lightweight" data structure, JSON is widely used in JS.

Google's parsing of JSON provides the Gson.jar package, which does not rely on any other jar packages; Since Android3.0 has been combined with the parser's functionality, the previous version was not available.

Findviewbyid (R.ID.PARSEBTN). Setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) {JSO
    Ndata = "[{\ name\": \ "michael\", \ "age\": 20},{\ "name\": \ "mike\", \ "age\": 21}] ";
  Mainactivity.parsejson (Jsondata);
}
}); public static void Parsejson (String jsonstr) {//If you want to parse JSON data, you first have to have a Jsonreader object jsonreader jsonreader = new Jsonreader (
  New StringReader (JSONSTR));
    try {//start traversing array (multiple JSON objects) Jsonreader.beginarray ();
      while (Jsonreader.hasnext ()) {//begins traversing the JSON object (containing multiple attributes) Jsonreader.beginobject ();
        while (Jsonreader.hasnext ()) {String tagName = Jsonreader.nextname ();
        if (tagname.equals ("name")) {System.out.println ("name-->" + jsonreader.nextstring ());
        }else if (tagname.equals (' age ')) {System.out.println ("Age-->" + jsonreader.nextstring ());
    }///Traversal JSON object end Jsonreader.endobject ();
  }//Traversal array end Jsonreader.endarray (); catch (IOException e) {E.printstaCktrace ();

 }
}

The steps to

Convert JSON format to a class are as follows:

First define a class public class User {public String getName () {return name;
  public void SetName (String name) {this.name = name;
  public int getage () {return age;
  public void Setage (int age) {this.age = age; }///The method of converting only one set of data Findviewbyid (r.id.parseobj). Setonclicklistener (New Onclicklistener () {@Override public void OnClick (
    View v {String jsondata = "{\ name\": \ "michael\", \ "age\": 20} ";
    Gson Gson = new Gson ();
    User user = Gson.fromjson (jsondata, User.class);
    SYSTEM.OUT.PRINTLN ("Name--->" + user.getname ());
  System.out.println ("Age---->" + user.getage ());
}
}); Methods for converting multiple sets of data Findviewbyid (R.ID.PARSEOBJS). Setonclicklistener (New Onclicklistener () {@Override public void OnClick (
    View v {String jsondata = "[{\ name\": \ "michael\", \ "age\": 20},{\ "name\": \ "mike\", \ "age\": 21}] ";
    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 ());

 }
  }
});

For more information on Android-related content readers can view the site topics: "Android File Operation tips Summary", "Android programming development of the SD card operation Summary", "Android Development introduction and Advanced Course", "Android Resources Operating Skills summary", " Android View tips Summary and a summary of the use of Android controls

I hope this article will help you with the Android program.

Related Article

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.