Android中解析JSON格式資料常見方法合集

來源:互聯網
上載者:User

標籤:

  待解析的JSON格式的檔案如下:

    [{"id":"5", "version":"1.0", "name":"xiaowang"},

    {"id":"10", "version":"2.0", "name":"lisi"}]

一、使用JSONObject來解析JSON資料

  官方提供的,所有不需要匯入第三方jar包;直接上代碼,如下:

 1 //方法一:使用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 }

  步驟解讀:

     定義一個JSON數組,用於將伺服器返回的資料傳入到一個JSONArray對象中; 然後迴圈遍曆這個JSONArray,

   從中取出每一個元素(JSONObject對象),接下來只需調用getString()方法即可將資料取出。

 

二、使用GSON

  使用該方法解析JSON資料,首先需要添加GSON的jar包;是:http://download.csdn.net/detail/a924571572/5824225

    需要匯入的jar包 

  下面是核心代碼:

//方法二:使用GSONprivate 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());    }}

  步驟解讀:

   根據JSON資料內容需要定義一個類,用存放資料,如App類:

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;    }      //......}

     如只有一組資料,則可以直接調用如下代碼     

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

     如果有多組資料,則需要藉助TypeToken將期望解析成的資料類型傳入fromJson()方法中:

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

    之後直接使用App對象的方法,如:getId、getName....即可擷取資料

  補充:

   TypeToken是什麼呢?

    TypeToken的使用非常簡單,如上面的代碼,只要將需要擷取類型的泛型類作為TypeToken的泛型參數構造一個匿名的子類,就可以通過getType()方法擷取到我們使用的泛型類的泛型參數類型。

 

三、使用Jackson

  第三方的工具知道該怎麼處理了吧,jar包的:http://wiki.fasterxml.com/JacksonDownload

    其中需要使用到:

      jackson-databind.jar   核心包(必須),提供基於“流模式”解析的API【JsonPaser(json流讀取),JsonGenerator(json流輸出)】

        jackson-annotations.jar   資料繫結包(可選),提供基於“對象綁定”和“樹模型”相關API。【ObjectMapper,JsonNode(樹節點)】

      jackson-core.jar  註解包(可選),提供註解功能。

   核心方法:

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

 

四、使用Fastjson
  不多說,直接上jar包:http://download.csdn.net/download/finaljia/5293875

  核心代碼:

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

 

Android中解析JSON格式資料常見方法合集

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.