Android中g(shù)son、jsonobject解析JSON的方法詳解
JSON的定義:
一種輕量級(jí)的數(shù)據(jù)交換格式,具有良好的可讀和便于快速編寫的特性。業(yè)內(nèi)主流技術(shù)為其提供了完整的解決方案(有點(diǎn)類似于正則表達(dá)式 ,獲得了當(dāng)今大部分語(yǔ)言的支持),從而可以在不同平臺(tái)間進(jìn)行數(shù)據(jù)交換。JSON采用兼容性很高的文本格式,同時(shí)也具備類似于C語(yǔ)言體系的行為。
JSON對(duì)象:
JSON中對(duì)象(Object)以"{"開(kāi)始, 以"}"結(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ù)組, 也就是說(shuō)可以存在嵌套的情況.
JSON數(shù)組:
JSON數(shù)組(array)以"["開(kāi)始, 以"]"結(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ò)開(kāi)發(fā)中常見(jiàn)的數(shù)據(jù)格式。解析JSON數(shù)據(jù)有多種方法。
1.1 使用官方自帶JSONObject
1.2 使用第三方開(kāi)源庫(kù),包括但不限于 GSON 、 FastJSON 、 Jackson ,本文主要介紹由Google提供的GSON庫(kù)的使用方法。
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 方法簡(jiǎn)介
•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的 簡(jiǎn)便之處 在于其可以將字符串 自動(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中實(shí)現(xiàn)長(zhǎng)按修改ListView對(duì)象的內(nèi)容
這篇文章主要給大家介紹了在Android中實(shí)現(xiàn)長(zhǎng)按修改ListView對(duì)象內(nèi)容的相關(guān)資料,文中給出了完整的示例代碼,相信對(duì)大家具有一定的參考價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-02-02Android仿QQ空間主頁(yè)面的實(shí)現(xiàn)
今天模仿安卓QQ空間,打開(kāi)程序的啟動(dòng)畫面和導(dǎo)航頁(yè)面我就不做了,大家可以模仿微信的那個(gè)做一下,很簡(jiǎn)單。這次主要做一下主頁(yè)面的實(shí)現(xiàn),感興趣的朋友可以參考下2013-01-01Android Activity Results API代替onActivityResul
說(shuō)到onActivityResult,我們已經(jīng)非常熟悉來(lái),通過(guò)在A activity啟動(dòng)B activity并且傳入數(shù)據(jù)到B中,然后在A中通過(guò)onActivityResult來(lái)接收B中返回的數(shù)據(jù)。在最新的activity-ktx的beta版本中,谷歌已經(jīng)廢棄了onActivityResult2022-09-09Android SQLite數(shù)據(jù)庫(kù)加密的操作方法
因?yàn)锳ndroid自帶的SQLite數(shù)據(jù)庫(kù)本身是沒(méi)有實(shí)現(xiàn)加密的,那我們?nèi)绾螌?shí)現(xiàn)對(duì)數(shù)據(jù)庫(kù)的加密呢?今天通過(guò)本文給大家介紹下Android SQLite數(shù)據(jù)庫(kù)加密的操作方法,一起看看吧2021-09-09Android實(shí)現(xiàn)簡(jiǎn)易計(jì)算功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)簡(jiǎn)易計(jì)算功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-06-06Android ApplicationContext接口深入分析
ApplicationContext是Spring應(yīng)用程序中的中央接口,由于繼承了多個(gè)組件,使得ApplicationContext擁有了許多Spring的核心功能,如獲取bean組件,注冊(cè)監(jiān)聽(tīng)事件,加載資源文件等2022-11-11Android studio 2020中的Android SDK 下載教程
這篇文章主要介紹了Android studio 2020中的Android SDK 下載教程,本文圖文并茂給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03在Android中使用WebSocket實(shí)現(xiàn)消息通信的方法詳解
這篇文章主要介紹了在Android中使用WebSocket實(shí)現(xiàn)消息通信的方法詳解,消息推送功能可以說(shuō)移動(dòng)APP不可缺少的功能之一,使用WebSocket實(shí)現(xiàn)消息推送功能。感興趣的可以了解一下2020-07-07