android原生操作json資料

2021-07-25 09:04:01 字數 2955 閱讀 9474

android原生操作json資料

主要是兩個類 jsonobject 操作物件 jonsarray操作json陣列

物件轉json

1   //建立學生物件 2 student student=new student; 3 student.setage(23); 4 student.setclazz("六年級"); 5 student.setname("王二麻子"); 6 //建立jsonobject 7 jsonobject jsonobject=new jsonobject; 8 //鍵值對賦值 9 jsonobject.put("age",student.getage); 10 jsonobject.put("name",student.getname); 11 jsonobject.put("clazz",student.getclazz); 12 //轉化成json字串 13 string json=jsonobject.tostring; 14 //輸出日誌 15 log.e("objecttojson",json);
log日誌顯示

json轉物件

新建乙個jsonobject 把json串通過構造方法賦值 這個jsonobject 物件就帶有json的值 然後建立物件 乙個乙個賦值 jsonobject 對於不同型別的值 有不同的get方法

1 jsonobject jsonobject=new jsonobject(json); 2 student student=new student; 3 student.setname(jsonobject.getstring("name")); 4 student.setclazz(jsonobject.getstring("clazz")); 5 student.setage(jsonobject.getint("age")); 6 log.e("jsontoobject",student.getname+"*****="+student.getclazz+"===="+student.getage);
list轉json

使用jonsarray

1 //建立乙個集合 2 liststudents=new arraylist; 3 students.add(student); 4 students.add(student); 5 //建立乙個jsonarray 6 jsonarray jsonarray=newjsonarray; 7 //遍歷學生集合 8 for(inti=0;ijson轉list

1 //建立jsonarray把json傳入 2 jsonarray jsonarray=new jsonarray(json); 3 //建立學生集合 4 student students=new arraylist; 5 log.e("beforejsontolist","集合長度"+students.size); 6 //遍歷jsonarray 7 for(inti=0;i注意 :在使用jsonobject和jsonarray的過程中是需要捕獲異常的

有沒有感覺很麻煩,這要是資料多了簡直是要累死人了

fastjson運算元據

物件轉json

1 //建立學生物件 2 student student=new student; 3 student.setclazz("一班"); 4 student.setage(23); 5 student.setname("李四"); 6 //將物件轉為json串 7 string json=json.tojsonstring(student); 8 log.e("objecttojson",json);

只有一句話 就完成了 簡單到爆有沒有 感謝馬雲粑粑!!!

json轉物件

1 //將json轉為物件 引數1json 引數2物件型別 2 student student=json.parseobject(json,student.class); 3 log.e("jsontoobject","**********"+student.getname);
同樣只有一句話 相對於android原生真是感人

list轉json

1 liststulist=new arraylist; 2 stulist.add(student); 3 stulist.add(student); 4 stulist.add(student); 5 //list集合轉json 6 json=json.tojsonstring(stulist); 7 log.e("listtojson","**********"+json);
集合中新增了三個同乙個物件 json字串的輸出 就變成了 ref, 很明顯這是引用第乙個物件 因為你新增了相同的物件 fastjson就不建立了 直接引用 這也是他號稱最快的原因

但是隨之而來的就有乙個問題 fastjson識別引用 其他的jar不識別 如果伺服器使用fastjson 客戶端使用gson 怎麼辦嘞

1,都使用fastjson

2.在轉json的時候設定一條屬性 禁用迴圈引用物件 就ok

1 json=json.tojsonstring(stulist,serializerfeature.disablecircularreferencedetect);
json轉list

1 stulist=json.parsearray(json,student.class); 2 student student1=stulist.get(0); 3 log.e("jsontolist","********************="+student1.getname);
有時候呢 並不需要物件裡的所有字段 這時候就可以設定乙個屬性過濾器 把你不需要的字段過濾掉

設定name過濾 請看log日誌

在介紹一種情況

定義了乙個泛型類

裡面有乙個學生物件 和乙個字串

把物件轉json

當我們要把這個json轉為物件的時候問題就來了

這時候就需要實現typereference類 把物件封裝一下

完美解決 凡是帶泛型的都可以使用typereference

最後給大家介紹乙個** 特別強大 會自動格式化json 如果有語法錯誤也會報錯滴

Android原生生成JSON與解析JSON

json資料是一種輕量級的資料交換格式,在 android 中通常應用於客戶端與伺服器互動之間的資料傳輸。像現在在網上有很多解析 json 資料的jar 包,但是歸根到底用的都是 android 原生解析 json 資料的方式,所以掌握 android 原生解析 json 資料的方法相當重要。下面分...

原生json解析庫手解JSON資料

我先看一下示例介面 我們看到我需要的video uri在很多層下面,所以我要用手將每一層撥開 洋蔥的bgm響起 我將網路請求的來的資料每層每層的撥開,再存入jsonarray中 jsonobject jsonobject new jsonobject respone jsonobject jsono...

android 原生http請求

向指定 url 傳送post方法的請求 param url 傳送請求的 url param param 請求引數,請求引數應該是 name1 value1 name2 value2 的形式。return 所代表遠端資源的響應結果 public static string sendpost strin...