android 筆記---JSON解析的兩種方法(JSONObject與GSON)

LucioNewber 10年前發布 | 21K 次閱讀 Gson JSON Android Android開發 移動開發

來自: http://www.cnblogs.com/gzdaijie/p/5189542.html

1.寫在前面

JSON數據是android網絡開發中常見的數據格式。解析JSON數據有多種方法。

1.1 使用官方自帶JSONObject

1.2 使用第三方開源庫,包括但不限于 GSON 、 FastJSON 、 Jackson ,本文主要介紹由Google提供的 GSON庫 的使用方法。

</div>

</div>

2.JSONObject的使用方法

2.1 示例代碼

    //org.json.JSONArray;
    //org.json.JSONObject;
    private void parseJSONWithJSONObject(String jsonData){
        try {
            //將json字符串jsonData裝入JSON數組,即JSONArray
            //jsonData可以是從文件中讀取,也可以從服務器端獲得
            JSONArray jsonArray = new JSONArray(jsonData);
            for (int i = 0; i< jsonArray.length(); i++) {
                //循環遍歷,依次取出JSONObject對象
                //用getInt和getString方法取出對應鍵值
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                int stu_no = jsonObject.getInt("stu_no");
                String stu_name = jsonObject.getString("stu_name");
                String stu_sex = jsonObject.getString("stu_sex");
                Log.d("MainActivity","stu_no: " + stu_no);
                Log.d("MainActivity","stu_name: " + stu_name);
                Log.d("MainActivity","stu_sex: " + stu_sex);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

2.2 字符串jsonData如下,圖為運行結果

[{ "stu_no":12345,"stu_name":"John","stu_sex":"male"
},{ "stu_no":12346,"stu_name":"Tom","stu_sex":"male"
},{"stu_no":12347,"stu_name":"Lily","stu_sex":"female"}]

3.GSON的使用方法

3.1 下載并安裝

  • GSON2.6.1下載地址 ,點擊即可下載。
  • 將下載的gson-2.6.1.jar復制到 項目目錄->app->libs 文件夾下

3.2 方法簡介

  • toJson(params1),將傳入對象轉換為字符串
  • fromJson(params1,params2),傳入兩個參數,將字符串params1轉換為params2指定的數據類型。

3.3 示例代碼

3.3.1 單個對象的解析

public class Student {
    private int stu_no;
    private String stu_name;
    private String stu_sex;

Student(int stu_no,String stu_name,String stu_sex){
    this.stu_no = stu_no;
    this.stu_name = stu_name;
    this.stu_sex = stu_sex;
}

}

// 序列化,將Student對象stu轉換為字符串str Student stu = new Student(123,"Tom","male"); Gson gson = new Gson(); String str = gson.toJson(stu);

//反序列化,將字符串轉換為Student對象 jsonData = "{ \"stu_no\":12345,\"stu_name\":\"John\",\"stu_sex\":\"male\" }"; Gson gson = new Gson(); Student student = gson.fromJson(jsonData,Student.class);</pre>

3.3.2 JSON數組的解析(原生類)

Gson gson = new Gson();
int[] ints = {1, 2, 3, 4, 5};
String[] strings = {"abc", "def", "ghi"};

//序列化(serialization) //將整數數組轉換為JSON數組 gson.toJson(ints); // ==> [1,2,3,4,5] //將字符串數組轉換為JSON數組 gson.toJson(strings); // ==> ["abc", "def", "ghi"]

// 反序列化(Deserialization) // 將JSON數組轉換為原生類數組 // ints2、string2與ints、strings相等 int[] ints2 = gson.fromJson("[1,2,3,4,5]", int[].class); String[] strings2 = gson.fromJson("[\"abc\", \"def\", \"ghi\"]",String[].class);</pre>

3.3.3 JSON數組的解析(自定義類)

//對于類似于2.2中的jsonData,包含3個Student對象
//與原生類不同,需要借助TypeToken獲得期望解析成的數據類型
//下列代碼運行后,students包含三個Student對象
Gson gson = new Gson();
List<Student> students;
students = gson.fromJson(jsonData, new TypeToken<List<Student>>(){}.getType()); // ==>[stu0,stu1,stu2]

3.4 更多方法

  • GSON的 簡便之處 在于其可以將字符串 自動映射 為原生或自定義對象,從而不需要手動編寫代碼進行解析。
  • GSON的 更多方法 可以閱讀GSON在github上的 用法介紹 ,README.md -> user guide。
 本文由用戶 LucioNewber 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!