The JSON-formatted files to be parsed are as follows:
[{"id": "5", "Version": "1.0", "Name": "Xiaowang"},
{"id": "Ten", "Version": "2.0", "name": "Lisi"}]
First, using Jsonobject to parse JSON data
Officially provided, all do not need to import third party jar package; directly on the code, as follows:
1 //method One: Use Jsonobject2 Private voidParsejsonwithjsonobject (String jsondata) {3 Try4 {5 Jsonarray Jsonarray =NewJsonarray (jsondata);6 for(inti=0; I < jsonarray.length (); i++) {7 jsonobject Jsonobject =Jsonarray. Getjsonobject (i);8String id = jsonobject.getString("id");9String name = Jsonobject.getString("Name");TenString Version = Jsonobect.getString("Version"); One ASYSTEM.OUT.PRINTLN ("id" + ID + "; name" + name + "; version" +version); - } - } the Catch(Exception e) - { - e.printstacktrace (); - } +}
Step Interpretation:
Defines a JSON array that is used to pass the data returned by the server into a Jsonarray object, and then loops through the Jsonarray,
Remove each element (jsonobject object ) from it, and then simply call the GetString () method to remove the data.
Second, the use of Gson
Use this method to parse JSON data, first need to add Gson jar package; Yes: http://download.csdn.net/detail/a924571572/5824225
Jar packages that need to be imported
Here's the core code :
//method Two: Use GsonPrivate voidParsejsonwithgson (String jsondata) { Gson Gson=NewGson (); List <App>Applist =Gson. Fromjson (Jsondata,New TypeToken<List<App>>() {}.gettype ()); for(Appapp: Applist) {System.out.println ("id" + app.getId() + "; name" + App.GetName() + "; version" +app.getversion()); }}
Step Interpretation:
Depending on the JSON data content, you need to define a class that holds the data, such as the App class:
Public class App { private String ID; Private String name; Private String version; Public String getId () { return ID; } Public void setId (String id) { this. id = ID; } //......}
If there is only one set of data, you can call the following code directly
New= Gson.gromjson (Jsondata, App.class);
If you have more than one set of data, you need to pass the expected parsed data type into the Fromjson () method with TypeToken:
New Typetoken<<list<app>>> (). GetType ());
Then directly use the method of the App object, such as: GetId, GetName .... You can get the data
Add :
What is TypeToken?
TypeToken is very simple to use, as the above code, as long as the need to get the type of generic class as TypeToken generic parameters to construct an anonymous subclass, you can use the GetType () method to get to the generic class we used the generic parameter type.
Iii. use of Jackson
Third-party tools know what to do with the jar package: Http://wiki.fasterxml.com/JacksonDownload
These need to be used to:
jackson-DataBind. Jar Core Package (required), provides an API based on "stream mode" parsingjsonpaser(JSON stream read),Jsongenerator (JSON stream output) "
jackson-annotations. The jar data binding package (optional), which provides APIs based on the object binding and tree model. "Objectmapper,jsonnode (tree node)"
Jackson-core. jar Annotation package (optional), providing annotation functionality.
Core Approach :
Public Static void Parsejsonwithjackson (String jsondata) { objectmappernew objectmapper (); Try { = mapper. ReadValue(Jsondata, App.class);
SYSTEM.OUT.PRINTLN ("id" + app.getId() + "; name" + App.getName() + "; version" + app . GetVersion());
Catch (Jsonparseexception e) {
Catch (Jsonmappingexception e) {
Catch (IOException e) {
Iv. Use of Fastjson
Not much to say, directly on the jar package: http://download.csdn.net/download/finaljia/5293875
Core Code :
Jsonarray Jarr = Jsonarray.Parsearray(Jsondata);//Json.parsearray (JSONSTR); for(Iteratoriterator =Jarr. iterator (); Iterator. Hasnext (); ) { jsonobject Job=(Jsonobject) iterator. Next (); String ID= Job.Get("id"). toString (); String name= Job.Get("Name"). toString (); String version= Job.Get("Version"). toString ();
System.out.println ("ID" + ID + "; name" + name + "; version" + version);}
Common methods for parsing JSON format data in Android collection