詳解Android原生json和fastjson的簡(jiǎn)單使用
android原生操作json數(shù)據(jù)
主要是兩個(gè)類 JSONObject 操作對(duì)象 JONSArray操作json數(shù)組
對(duì)象轉(zhuǎn)json
//創(chuàng)建學(xué)生對(duì)象 Student student=new Student(); student.setAge(23); student.setClazz("六年級(jí)"); student.setName("王二麻子"); //創(chuàng)建JSONObject JSONObject jsonObject=new JSONObject(); //鍵值對(duì)賦值 jsonObject.put("age",student.getAge()); jsonObject.put("name",student.getName()); jsonObject.put("clazz",student.getClazz()); //轉(zhuǎn)化成json字符串 String json=jsonObject.toString(); //輸出日志 Log.e("ObjectToJson",json);
Log日志顯示
json轉(zhuǎn)對(duì)象
新建一個(gè)JSONObject 把json串通過構(gòu)造方法賦值 這個(gè)JSONObject 對(duì)象就帶有json的值 然后創(chuàng)建對(duì)象 一個(gè)一個(gè)賦值 JSONObject 對(duì)于不同類型的值 有不同的get方法
JSONObject jsonObject=new JSONObject(json); Student student=new Student(); student.setName(jsonObject.getString("name")); student.setClazz(jsonObject.getString("clazz")); student.setAge(jsonObject.getInt("age")); Log.e("JsonToObject",student.getName()+"======"+student.getClazz()+"===="+student.getAge());
List轉(zhuǎn)json
使用JONSArray
//創(chuàng)建一個(gè)集合 List<Student> students=new ArrayList<Student>(); students.add(student); students.add(student); //創(chuàng)建一個(gè)JSONArray JSONArray jsonArray=newJSONArray(); //遍歷學(xué)生集合 for(inti=0;i<students.size();i++){ //獲取學(xué)生對(duì)象 Studentstu=students.get(i); //創(chuàng)建JSONObject JSONObject jo=newJSONObject(); //賦值 jo.put("age",stu.getAge()); jo.put("name",stu.getName()); jo.put("clazz",stu.getClazz()); //把JSONObject 添加到JSONArray jsonArray.put(jo); } //toString轉(zhuǎn)為json String json=jsonArray.toString(); Log.e("ListToJson",json);
json轉(zhuǎn)List
//創(chuàng)建JSONArray把json傳入 JSONArray jsonArray=new JSONArray(json); //創(chuàng)建學(xué)生集合 Student students=new ArrayList<Student>(); Log.e("BeforeJsonToList","集合長(zhǎng)度"+students.size()); //遍歷jsonArray for(inti=0;i<jsonArray.length();i++){ //獲取JSONObject對(duì)象 JSONObject jsonObject=jsonArray.getJSONObject(i); //創(chuàng)建學(xué)生對(duì)象 Student stu=new Student(); //賦值 jsonObject.put("age",stu.getAge()); jsonObject.put("name",stu.getName()); jsonObject.put("clazz",stu.getClazz()); //把學(xué)生對(duì)象添加到集合中 students.add(stu); } Log.e("AfterJsonToList","集合長(zhǎng)度"+students.size());
注意 :在使用JSONObject和JSONArray的過程中是需要捕獲異常的
有沒有感覺很麻煩,這要是數(shù)據(jù)多了簡(jiǎn)直是要累死人了
變簡(jiǎn)單的方法就是下載一個(gè)號(hào)稱史上最快json操作的fastjson.jar 阿里出品 然后使用就簡(jiǎn)單了
FastJson操作數(shù)據(jù)
對(duì)象轉(zhuǎn)json
//創(chuàng)建學(xué)生對(duì)象 Student student=new Student(); student.setClazz("一班"); student.setAge(23); student.setName("李四"); //將對(duì)象轉(zhuǎn)為json串 String json=JSON.toJSONString(student); Log.e("ObjectToJson",json);
只有一句話 就完成了 簡(jiǎn)單到爆有沒有 感謝馬云粑粑?。?!
json轉(zhuǎn)對(duì)象
//將json轉(zhuǎn)為對(duì)象 參數(shù)1json 參數(shù)2對(duì)象類型 Student student=JSON.parseObject(json,Student.class); Log.e("JsonToObject","=========="+student.getName());
同樣只有一句話 相對(duì)于android原生真是感人
list轉(zhuǎn)json
List<Student>stuList=new ArrayList<Student>(); stuList.add(student); stuList.add(student); stuList.add(student); //List集合轉(zhuǎn)json json=JSON.toJSONString(stuList); Log.e("ListToJson","=========="+json);
集合中添加了三個(gè)同一個(gè)對(duì)象 json字符串的輸出 就變成了 ref,{0} 很明顯這是引用第一個(gè)對(duì)象 因?yàn)槟闾砑恿讼嗤膶?duì)象 fastjson就不創(chuàng)建了 直接引用 這也是他號(hào)稱最快的原因
但是隨之而來(lái)的就有一個(gè)問題 fastjson識(shí)別引用 其他的jar不識(shí)別 如果服務(wù)器使用fastjson 客戶端使用gson 怎么辦嘞
1.都使用fastjson
2.在轉(zhuǎn)json的時(shí)候設(shè)置一條屬性 禁用循環(huán)引用對(duì)象 就ok
json=JSON.toJSONString(stuList,SerializerFeature.DisableCircularReferenceDetect);
json轉(zhuǎn)list
stuList=JSON.parseArray(json,Student.class); Student student1=stuList.get(0); Log.e("JsonToList","====================="+student1.getName());
有時(shí)候呢 并不需要對(duì)象里的所有字段 這時(shí)候就可以設(shè)置一個(gè)屬性過濾器 把你不需要的字段過濾掉
//過濾字段 屬性過濾器PropertyFilter json=JSON.toJSONString(stuList, new PropertyFilter() { @Override//參數(shù)1 正在被過濾的對(duì)象 參數(shù)2 過濾的屬性名 參數(shù)3 屬性值 public boolean apply(Object o, String s, Object o1) { Log.e("PropertyFilter",o+"======"+s+"==============="+o1); if (s.equals("name")){ return false; }else{ return true; } } }); Log.e("PropertyFilter",json);
設(shè)置name過濾 請(qǐng)看log日志
在介紹一種情況
定義了一個(gè)泛型類
里面有一個(gè)學(xué)生對(duì)象 和一個(gè)字符串
把對(duì)象轉(zhuǎn)json
當(dāng)我們要把這個(gè)json轉(zhuǎn)為對(duì)象的時(shí)候問題就來(lái)了
這時(shí)候就需要實(shí)現(xiàn)TypeReference類 把對(duì)象封裝一下
完美解決 凡是帶泛型的都可以使用TypeReference
最后給大家介紹一個(gè)網(wǎng)站 http://json.cn/ 特別強(qiáng)大 會(huì)自動(dòng)格式化json 如果有語(yǔ)法錯(cuò)誤也會(huì)報(bào)錯(cuò)滴
以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,同時(shí)也希望多多支持腳本之家!
相關(guān)文章
Android實(shí)現(xiàn)沉浸式狀態(tài)欄
這篇文章主要為大家詳細(xì)介紹了Android沉浸式狀態(tài)欄的實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11android 更改TextView中任意位置字體大小和顏色的方法
下面小編就為大家分享一篇android 更改TextView中任意位置字體大小和顏色的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2018-01-01Android studio 混淆+打包+驗(yàn)證是否成功
本文主要介紹了Android studio 混淆+打包+驗(yàn)證是否成功的相關(guān)知識(shí),具有很好的參考價(jià)值。下面跟著小編一起來(lái)看下吧2017-03-03詳解Android如何設(shè)計(jì)一個(gè)全局可調(diào)用的ViewModel對(duì)象
很多時(shí)候我們需要維護(hù)一個(gè)全局可用的ViewModel,因?yàn)檫@樣可以維護(hù)全局同一份數(shù)據(jù)源,且方便使用協(xié)程綁定App的生命周期,那如何設(shè)計(jì)全局可用的ViewModel對(duì)象,文中介紹的非常詳細(xì),需要的朋友可以參考下2023-05-05Android自定義控件之圓形/圓角的實(shí)現(xiàn)代碼
這篇文章主要為大家詳細(xì)介紹了Android自定義控件之圓形/圓角的實(shí)現(xiàn)代碼,感興趣的小伙伴們可以參考一下2016-03-03Android ListView 實(shí)現(xiàn)上拉加載的示例代碼
這篇文章主要介紹了Android ListView 實(shí)現(xiàn)上拉加載的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧2018-07-07一款非常簡(jiǎn)單酷炫的LoadingView動(dòng)畫效果
這篇文章主要為大家詳細(xì)介紹了一款非常簡(jiǎn)單酷炫的LoadingView動(dòng)畫效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-08-08