詳解Android原生json和fastjson的簡單使用
android原生操作json數(shù)據(jù)
主要是兩個類 JSONObject 操作對象 JONSArray操作json數(shù)組
對象轉(zhuǎn)json
//創(chuàng)建學生對象
Student student=new Student();
student.setAge(23);
student.setClazz("六年級");
student.setName("王二麻子");
//創(chuàng)建JSONObject
JSONObject jsonObject=new JSONObject();
//鍵值對賦值
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)對象
新建一個JSONObject 把json串通過構(gòu)造方法賦值 這個JSONObject 對象就帶有json的值 然后創(chuàng)建對象 一個一個賦值 JSONObject 對于不同類型的值 有不同的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)建一個集合
List<Student> students=new ArrayList<Student>();
students.add(student);
students.add(student);
//創(chuàng)建一個JSONArray
JSONArray jsonArray=newJSONArray();
//遍歷學生集合
for(inti=0;i<students.size();i++){
//獲取學生對象
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)建學生集合
Student students=new ArrayList<Student>();
Log.e("BeforeJsonToList","集合長度"+students.size());
//遍歷jsonArray
for(inti=0;i<jsonArray.length();i++){
//獲取JSONObject對象
JSONObject jsonObject=jsonArray.getJSONObject(i);
//創(chuàng)建學生對象
Student stu=new Student();
//賦值
jsonObject.put("age",stu.getAge());
jsonObject.put("name",stu.getName());
jsonObject.put("clazz",stu.getClazz());
//把學生對象添加到集合中
students.add(stu);
}
Log.e("AfterJsonToList","集合長度"+students.size());

注意 :在使用JSONObject和JSONArray的過程中是需要捕獲異常的
有沒有感覺很麻煩,這要是數(shù)據(jù)多了簡直是要累死人了
變簡單的方法就是下載一個號稱史上最快json操作的fastjson.jar 阿里出品 然后使用就簡單了
FastJson操作數(shù)據(jù)
對象轉(zhuǎn)json
//創(chuàng)建學生對象
Student student=new Student();
student.setClazz("一班");
student.setAge(23);
student.setName("李四");
//將對象轉(zhuǎn)為json串
String json=JSON.toJSONString(student);
Log.e("ObjectToJson",json);
只有一句話 就完成了 簡單到爆有沒有 感謝馬云粑粑?。?!

json轉(zhuǎn)對象
//將json轉(zhuǎn)為對象 參數(shù)1json 參數(shù)2對象類型
Student student=JSON.parseObject(json,Student.class);
Log.e("JsonToObject","=========="+student.getName());
同樣只有一句話 相對于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);
集合中添加了三個同一個對象 json字符串的輸出 就變成了 ref,{0} 很明顯這是引用第一個對象 因為你添加了相同的對象 fastjson就不創(chuàng)建了 直接引用 這也是他號稱最快的原因
但是隨之而來的就有一個問題 fastjson識別引用 其他的jar不識別 如果服務(wù)器使用fastjson 客戶端使用gson 怎么辦嘞
1.都使用fastjson
2.在轉(zhuǎn)json的時候設(shè)置一條屬性 禁用循環(huán)引用對象 就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è)置一個屬性過濾器 把你不需要的字段過濾掉
//過濾字段 屬性過濾器PropertyFilter
json=JSON.toJSONString(stuList, new PropertyFilter() {
@Override//參數(shù)1 正在被過濾的對象 參數(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過濾 請看log日志

在介紹一種情況
定義了一個泛型類
里面有一個學生對象 和一個字符串

把對象轉(zhuǎn)json

當我們要把這個json轉(zhuǎn)為對象的時候問題就來了

這時候就需要實現(xiàn)TypeReference類 把對象封裝一下

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

最后給大家介紹一個網(wǎng)站 http://json.cn/ 特別強大 會自動格式化json 如果有語法錯誤也會報錯滴
以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對大家的學習或者工作能帶來一定的幫助,同時也希望多多支持腳本之家!
相關(guān)文章
android 更改TextView中任意位置字體大小和顏色的方法
下面小編就為大家分享一篇android 更改TextView中任意位置字體大小和顏色的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01
詳解Android如何設(shè)計一個全局可調(diào)用的ViewModel對象
很多時候我們需要維護一個全局可用的ViewModel,因為這樣可以維護全局同一份數(shù)據(jù)源,且方便使用協(xié)程綁定App的生命周期,那如何設(shè)計全局可用的ViewModel對象,文中介紹的非常詳細,需要的朋友可以參考下2023-05-05
Android ListView 實現(xiàn)上拉加載的示例代碼
這篇文章主要介紹了Android ListView 實現(xiàn)上拉加載的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-07-07

