Parsing JSON data in Android

Source: Internet
Author: User

Problems with parsing JSON are often encountered in development

Here are some ways to summarize the parsing:

Way One:

JSON data:

Private String Jsondata = "[{\" name\ ": \" michael\ ", \" age\ ": 20},{\" name\ ": \" mike\ ", \" age\ ": 21}]";
Methods for parsing Jsondata

try {//If you need to parse JSON data, first generate 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.nextint ()}} Reader.endobject ();} Reader.endarray ();} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();}}

Method Two:

Private String jsondata= "{\" name\ ": \" michael\ ", \" age\ ": 20}";

Parsing methods

Gson gson=new Gson (); User User=gson.fromjson (Jsondata, User.class); System.out.println ("name-->" +user.getname ()); System.out.println ("age-->" +user.getage ());

You need to import Gson-2.2.4.jar here

public class User {private string name;private int age;public string getName () {return name;} public void SetName (String name) {this.name = name;} public int getage () {return age;} public void Setage (int.) {this.age = age;}}

This is the way to parse the more simple data

Method Three:

Private 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 ());}

In fact, in addition to these three ways there are other methods, and so later used to summarize.


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.