Android中g(shù)son、jsonobject解析JSON的方法詳解
JSON的定義:
一種輕量級的數(shù)據(jù)交換格式,具有良好的可讀和便于快速編寫的特性。業(yè)內(nèi)主流技術(shù)為其提供了完整的解決方案(有點類似于正則表達式 ,獲得了當(dāng)今大部分語言的支持),從而可以在不同平臺間進行數(shù)據(jù)交換。JSON采用兼容性很高的文本格式,同時也具備類似于C語言體系的行為。
JSON對象:
JSON中對象(Object)以"{"開始, 以"}"結(jié)束. 對象中的每一個item都是一個key-value對, 表現(xiàn)為"key:value"的形式, key-value對之間使用逗號分隔. 如:{"name":"coolxing", "age"=24, "male":true, "address":{"street":"huiLongGuan", "city":"beijing", "country":"china"}}. JSON對象的key只能是string類型的, 而value可以是string, number, false, true, null, Object對象甚至是array數(shù)組, 也就是說可以存在嵌套的情況.
JSON數(shù)組:
JSON數(shù)組(array)以"["開始, 以"]"結(jié)束, 數(shù)組中的每一個元素可以是string, number, false, true, null, Object對象甚至是array數(shù)組, 數(shù)組間的元素使用逗號分隔. 如["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對象 //用getInt和getString方法取出對應(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如下,圖為運行結(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ù)制到 項目目錄->app->libs 文件夾下
3.2 方法簡介
•toJson(params1),將傳入對象轉(zhuǎn)換為字符串
•fromJson(params1,params2),傳入兩個參數(shù),將字符串params1轉(zhuǎn)換為params2指定的數(shù)據(jù)類型。
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轉(zhuǎn)換為字符串str Student stu = new Student(123,"Tom","male"); Gson gson = new Gson(); String str = gson.toJson(stu); //反序列化,將字符串轉(zhuǎn)換為Student對象 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ù)組的解析(自定義類)
//對于類似于2.2中的jsonData,包含3個Student對象 //與原生類不同,需要借助TypeToken獲得期望解析成的數(shù)據(jù)類型 //下列代碼運行后,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。
以上內(nèi)容給大家介紹了Android中g(shù)son、jsonobject解析JSON的方法詳解,希望對大家有所幫助。
- JSONObject使用方法詳解
- java使用JSONObject實例
- java json字符串轉(zhuǎn)JSONObject和JSONArray以及取值的實例
- JSON字符串轉(zhuǎn)換JSONObject和JSONArray的方法
- Java代碼實現(xiàn)Map和Object互轉(zhuǎn)及Map和Json互轉(zhuǎn)
- 淺析Java中JSONObject和JSONArray使用
- SpringMVC restful 注解之@RequestBody進行json與object轉(zhuǎn)換
- JSONObject與JSONArray的使用
- 詳解JSONObject和JSONArray區(qū)別及基本用法
- JSON.parseObject和JSON.toJSONString實例詳解
相關(guān)文章
Android中實現(xiàn)長按修改ListView對象的內(nèi)容
這篇文章主要給大家介紹了在Android中實現(xiàn)長按修改ListView對象內(nèi)容的相關(guān)資料,文中給出了完整的示例代碼,相信對大家具有一定的參考價值,需要的朋友們下面來一起看看吧。2017-02-02Android Activity Results API代替onActivityResul
說到onActivityResult,我們已經(jīng)非常熟悉來,通過在A activity啟動B activity并且傳入數(shù)據(jù)到B中,然后在A中通過onActivityResult來接收B中返回的數(shù)據(jù)。在最新的activity-ktx的beta版本中,谷歌已經(jīng)廢棄了onActivityResult2022-09-09Android SQLite數(shù)據(jù)庫加密的操作方法
因為Android自帶的SQLite數(shù)據(jù)庫本身是沒有實現(xiàn)加密的,那我們?nèi)绾螌崿F(xiàn)對數(shù)據(jù)庫的加密呢?今天通過本文給大家介紹下Android SQLite數(shù)據(jù)庫加密的操作方法,一起看看吧2021-09-09Android ApplicationContext接口深入分析
ApplicationContext是Spring應(yīng)用程序中的中央接口,由于繼承了多個組件,使得ApplicationContext擁有了許多Spring的核心功能,如獲取bean組件,注冊監(jiān)聽事件,加載資源文件等2022-11-11Android studio 2020中的Android SDK 下載教程
這篇文章主要介紹了Android studio 2020中的Android SDK 下載教程,本文圖文并茂給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-03-03在Android中使用WebSocket實現(xiàn)消息通信的方法詳解
這篇文章主要介紹了在Android中使用WebSocket實現(xiàn)消息通信的方法詳解,消息推送功能可以說移動APP不可缺少的功能之一,使用WebSocket實現(xiàn)消息推送功能。感興趣的可以了解一下2020-07-07