Collection of common methods for parsing JSON data in Android, androidjson
The JSON format file to be parsed is as follows:
[{"Id": "5", "version": "1.0", "name": "xiaowang "},
{"Id": "10", "version": "2.0", "name": "lisi"}]
1. Use JSONObject to parse JSON data
Officially provided, all third-party jar packages do not need to be imported; directly add the Code as follows:
1 // Method 1: Use JSONObject 2 private void parseJSONWithJSONObject (String JsonData) {3 try 4 {5JSONArrayJsonArray = new JSONArray (jsonData); 6 for (int I = 0; I <jsonArray. length (); I ++) {7JSONObjectJsonObject = jsonArray. getJSONObject (I); 8 String id = jsonObject. getString ("id"); 9 String name = jsonObject. getString ("name"); 10 String version = jsonObect. getString ("version"); 11 12 System. out. println ("id" + id + "; name" + name + "; version" + version); 13} 14} 15 catch (Exception e) 16 {17 e. printStackTrace (); 18} 19}
Steps:
Define a JSON array to pass the data returned by the server to a JSONArray object. Then, traverse the JSONArray cyclically,
Extract each element (JSONObject object) from it. Next, you only need to call the getString () method to retrieve the data.
Ii. Use GSON
To use this method to parse JSON data, first you need to add the jar package of GSON; is: http://download.csdn.net/detail/a924571572/5824225
Jar package to be imported
The following is the core code:
// Method 2: Use GSONprivate void parseJSONWithGSON (String JsonData ){GsonGson = new Gson ();List<App>Applist = gson.FromJson(JsonData, new TypeToken <List <App> (){}. getType (); for (App app: applist) {System. out. println ("id" + app. getId () + "; name" + app. getName () + "; version" + app. getVersion ());}}
Steps:
Define a Class Based on the JSON data content to store 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 group of data, you can directly call the following code:
GSON gson = new GSON();App app = gson.gromJson(jsonData, App.class);
If there are multiple groups of data, you need to use TypeToken to pass the expected data type to the fromJson () method:
List<App> app = gson.fromJson(jsonData, new TypeToken<<List<App>>> ().getType());
Then, you can directly use the method of the App object, such as getId and getName... to obtain the data.
Supplement:
What is TypeToken?
The use of TypeToken is very simple.Generic type to be retrievedConstruct an anonymous subclass as a generic parameter of TypeToken, and you can use the getType () method to obtain the generic parameter type of the generic class we use.
Iii. Use Jackson
Third-party tools know how to deal with it, jar package: http://wiki.fasterxml.com/JacksonDownload
Here, you need to use:
Jackson-DatabindThe. jar core package (required) provides APIs for parsing based on the "stream mode 【JsonPaser(Json stream reading ),JsonGenerator(Json stream output )]
Jackson-Annotations. Jar data binding package (optional) provides APIs based on "Object binding" and "tree model. [ObjectMapper, JsonNode (tree node )]
Jackson-Core. Jar annotation package (optional), which provides the annotation function.
Core methods:
public static void parseJSONWithJackson(String jsonData) { ObjectMapper mapper = new ObjectMapper(); try { App app = mapper.readValue(jsonData, App.class);
System.out.println("id" + app.getId() + ";name" + app.getName() + ";version" + app.getVersion());
} catch (JsonParseException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
4. Use Fastjson
Not much said, directly on the jar package: http://download.csdn.net/download/finaljia/5293875
Core code:
JSONArray jarr = JSONArray.parseArray(jsonData); //JSON.parseArray(jsonStr); for (Iterator iterator = 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); }