Collection of common methods for parsing JSON format data in Android

The files in JSON format to be parsed are 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 do not need to import third-party jar packages; directly enter the code, as follows:

 

 1 // Method 1: Use JSONObject
 2 private void parseJSONWithJSONObject(String JsonData) {
 3     try
 4     {
 5         JSONArray jsonArray = new JSONArray(jsonData);
 6         for (int i=0; i < jsonArray.length(); i++)    {
 7             JSONObject jsonObject = 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 }

 

  Interpretation of steps:

     Define a JSON array, used to pass the data returned by the server into a JSONArray object; then loop through the JSONArray,

   Take out each element (JSONObject object) from it, and then just call the getString () method to get the data.

 

Second, use GSON

  To use this method to parse JSON data, you first need to add the GSON jar package; the download address is: http://download.csdn.net/detail/a924571572/5824225

    The jar package to be imported is shown in the figure: 

  Here is the core code:

 

// Method 2: Use GSON
private void parseJSONWithGSON(String JsonData) {
    Gson gson = 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());
    }
}

 

  Interpretation of steps:

   According to the JSON data content, you need to define a class 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 set of data, you can directly call the following code     

GSON gson = new GSON ();
App app = gson.fromJson(jsonData, App.class);

     If there are multiple sets of data, you need to use TypeToken to pass the expected data type into the fromJson () method:

List<App> app = gson.fromJson(jsonData, new TypeToken<<List<App>>> ().getType());

    Then use the methods of the App object directly, such as: getId, getName ... to get the data

  supplement:

   What is TypeToken?

    The use of TypeToken is very simple. As the above code, as long as the generic class that needs to get the type is used as a generic parameter of TypeToken to construct an anonymous subclass, we can get the generic of the generic class we use through the getType () method. Type parameter type.

 

Three, use Jackson

  Third-party tools know how to deal with it, the download address of the jar package: http://wiki.fasterxml.com/JacksonDownload

    Which need to use:

      jackson- databind .jar core package (required), providing API based on "stream mode" analysis [ JsonPaser (json stream read), JsonGenerator (json stream output)]

        jackson- annotations .jar data binding package (optional), provides related APIs based on "object binding" and "tree model". [ObjectMapper, JsonNode (tree node)]

      jackson- core .jar annotation package (optional), providing annotation function.

   Core method:

 

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

Copy code

 

Four, use Fastjson
  not much, just go to the jar package download address: 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); 
}  

 

Published 10 original articles · Like 11 · Visits 20,000+

Guess you like

Origin blog.csdn.net/u013323018/article/details/83098964