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.