Android中g(shù)son、jsonobject解析JSON的方法詳解
JSON的定義:
一種輕量級(jí)的數(shù)據(jù)交換格式,具有良好的可讀和便于快速編寫的特性。業(yè)內(nèi)主流技術(shù)為其提供了完整的解決方案(有點(diǎn)類似于正則表達(dá)式 ,獲得了當(dāng)今大部分語言的支持),從而可以在不同平臺(tái)間進(jìn)行數(shù)據(jù)交換。JSON采用兼容性很高的文本格式,同時(shí)也具備類似于C語言體系的行為。
JSON對(duì)象:
JSON中對(duì)象(Object)以"{"開始, 以"}"結(jié)束. 對(duì)象中的每一個(gè)item都是一個(gè)key-value對(duì), 表現(xiàn)為"key:value"的形式, key-value對(duì)之間使用逗號(hào)分隔. 如:{"name":"coolxing", "age"=24, "male":true, "address":{"street":"huiLongGuan", "city":"beijing", "country":"china"}}. JSON對(duì)象的key只能是string類型的, 而value可以是string, number, false, true, null, Object對(duì)象甚至是array數(shù)組, 也就是說可以存在嵌套的情況.
JSON數(shù)組:
JSON數(shù)組(array)以"["開始, 以"]"結(jié)束, 數(shù)組中的每一個(gè)元素可以是string, number, false, true, null, Object對(duì)象甚至是array數(shù)組, 數(shù)組間的元素使用逗號(hào)分隔. 如["coolxing", 24, {"street":"huiLongGuan", "city":"beijing", "country":"china"}].
1.前言
JSON數(shù)據(jù)是android網(wǎng)絡(luò)開發(fā)中常見的數(shù)據(jù)格式。解析JSON數(shù)據(jù)有多種方法。
1.1 使用官方自帶JSONObject
1.2 使用第三方開源庫,包括但不限于 GSON 、 FastJSON 、 Jackson ,本文主要介紹由Google提供的GSON庫的使用方法。
2.JSONObject的使用方法
2.1 示例代碼
//org.json.JSONArray;
//org.json.JSONObject;
private void parseJSONWithJSONObject(String jsonData){
try {
//將json字符串jsonData裝入JSON數(shù)組,即JSONArray
//jsonData可以是從文件中讀取,也可以從服務(wù)器端獲得
JSONArray jsonArray = new JSONArray(jsonData);
for (int i = 0; i< jsonArray.length(); i++) {
//循環(huán)遍歷,依次取出JSONObject對(duì)象
//用getInt和getString方法取出對(duì)應(yīng)鍵值
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如下,圖為運(yùn)行結(jié)果
[{ "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 下載并安裝
•將下載的gson-2.6.1.jar復(fù)制到 項(xiàng)目目錄->app->libs 文件夾下

3.2 方法簡介
•toJson(params1),將傳入對(duì)象轉(zhuǎn)換為字符串
•fromJson(params1,params2),傳入兩個(gè)參數(shù),將字符串params1轉(zhuǎn)換為params2指定的數(shù)據(jù)類型。
3.3 示例代碼
3.3.1 單個(gè)對(duì)象的解析
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對(duì)象stu轉(zhuǎn)換為字符串str
Student stu = new Student(123,"Tom","male");
Gson gson = new Gson();
String str = gson.toJson(stu);
//反序列化,將字符串轉(zhuǎn)換為Student對(duì)象
jsonData = "{ \"stu_no\":12345,\"stu_name\":\"John\",\"stu_sex\":\"male\" }";
Gson gson = new Gson();
Student student = gson.fromJson(jsonData,Student.class);
3.3.2 JSON數(shù)組的解析(原生類)
Gson gson = new Gson();
int[] ints = {1, 2, 3, 4, 5};
String[] strings = {"abc", "def", "ghi"};
//序列化(serialization)
//將整數(shù)數(shù)組轉(zhuǎn)換為JSON數(shù)組
gson.toJson(ints); // ==> [1,2,3,4,5]
//將字符串?dāng)?shù)組轉(zhuǎn)換為JSON數(shù)組
gson.toJson(strings); // ==> ["abc", "def", "ghi"]
// 反序列化(Deserialization)
// 將JSON數(shù)組轉(zhuǎn)換為原生類數(shù)組
// ints2、string2與ints、strings相等
int[] ints2 = gson.fromJson("[1,2,3,4,5]", int[].class);
String[] strings2 = gson.fromJson("[\"abc\", \"def\", \"ghi\"]",String[].class);
3.3.3 JSON數(shù)組的解析(自定義類)
//對(duì)于類似于2.2中的jsonData,包含3個(gè)Student對(duì)象
//與原生類不同,需要借助TypeToken獲得期望解析成的數(shù)據(jù)類型
//下列代碼運(yùn)行后,students包含三個(gè)Student對(duì)象
Gson gson = new Gson();
List<Student> students;
students = gson.fromJson(jsonData, new TypeToken<List<Student>>(){}.getType()); // ==>[stu0,stu1,stu2]
3.4 更多方法
•GSON的 簡便之處 在于其可以將字符串 自動(dòng)映射 為原生或自定義對(duì)象,從而不需要手動(dòng)編寫代碼進(jìn)行解析。
•GSON的 更多方法 可以閱讀GSON在github上的用法介紹,README.md -> user guide。
以上內(nèi)容給大家介紹了Android中g(shù)son、jsonobject解析JSON的方法詳解,希望對(duì)大家有所幫助。
- JSONObject使用方法詳解
- java使用JSONObject實(shí)例
- java json字符串轉(zhuǎn)JSONObject和JSONArray以及取值的實(shí)例
- JSON字符串轉(zhuǎn)換JSONObject和JSONArray的方法
- Java代碼實(shí)現(xiàn)Map和Object互轉(zhuǎn)及Map和Json互轉(zhuǎn)
- 淺析Java中JSONObject和JSONArray使用
- SpringMVC restful 注解之@RequestBody進(jìn)行json與object轉(zhuǎn)換
- JSONObject與JSONArray的使用
- 詳解JSONObject和JSONArray區(qū)別及基本用法
- JSON.parseObject和JSON.toJSONString實(shí)例詳解
相關(guān)文章
Android 自定義一套 Dialog通用提示框 (代碼庫)
這篇文章主要介紹了Android 自定義一套 Dialog通用提示框 (代碼庫),需要的朋友可以參考下2017-04-04
Android開發(fā)準(zhǔn)確獲取手機(jī)IP地址的兩種方式
這篇文章主要介紹了Android開發(fā)準(zhǔn)確獲取手機(jī)IP地址的兩種方式,需要的朋友可以參考下2020-03-03
android實(shí)現(xiàn)數(shù)獨(dú)游戲機(jī)器人
這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)數(shù)獨(dú)游戲機(jī)器人,文中安裝步驟介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
Android 判斷ip地址合法實(shí)現(xiàn)代碼
這篇文章主要介紹了Android 判斷ip地址合法實(shí)現(xiàn)代碼的相關(guān)資料,需要的朋友可以參考下2017-06-06
Android編程獲取地理位置的經(jīng)度和緯度實(shí)例
這篇文章主要介紹了Android編程獲取地理位置的經(jīng)度和緯度實(shí)現(xiàn)方法,結(jié)合實(shí)例形式詳細(xì)分析了Android操作系統(tǒng)服務(wù)調(diào)用GPS實(shí)現(xiàn)定位的相關(guān)技巧,需要的朋友可以參考下2016-01-01
Android實(shí)現(xiàn)網(wǎng)易新聞客戶端首頁效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)網(wǎng)易新聞客戶端首頁效果的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11

