android原生操作json數據
主要是兩個類 JSONObject 操作對象 JONSArray操作json數組
對象轉json
//創建學生對象 Student student=new Student(); student.setAge(23); student.setClazz("六年級"); student.setName("王二麻子"); //創建JSONObject JSONObject jsonObject=new JSONObject(); //鍵值對賦值 jsonObject.put("age",student.getAge()); jsonObject.put("name",student.getName()); jsonObject.put("clazz",student.getClazz()); //轉化成json字符串 String json=jsonObject.toString(); //輸出日志 Log.e("ObjectToJson",json);Log日志顯示

json轉對象
新建一個JSONObject 把json串通過構造方法賦值 這個JSONObject 對象就帶有json的值 然后創建對象 一個一個賦值 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轉json
使用JONSArray
//創建一個集合List<Student> students=new ArrayList<Student>();students.add(student);students.add(student);//創建一個JSONArray JSONArray jsonArray=newJSONArray();//遍歷學生集合for(inti=0;i<students.size();i++){ //獲取學生對象 Studentstu=students.get(i); //創建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轉為jsonString json=jsonArray.toString();Log.e("ListToJson",json);
json轉List
//創建JSONArray把json傳入JSONArray jsonArray=new JSONArray(json);//創建學生集合Student students=new ArrayList<Student>();Log.e("BeforeJsonToList","集合長度"+students.size());//遍歷jsonArrayfor(inti=0;i<jsonArray.length();i++){ //獲取JSONObject對象 JSONObject jsonObject=jsonArray.getJSONObject(i); //創建學生對象 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的過程中是需要捕獲異常的
有沒有感覺很麻煩,這要是數據多了簡直是要累死人了
變簡單的方法就是下載一個號稱史上最快json操作的fastjson.jar 阿里出品 然后使用就簡單了
FastJson操作數據
對象轉json
//創建學生對象Student student=new Student();student.setClazz("一班");student.setAge(23);student.setName("李四");//將對象轉為json串String json=JSON.toJSONString(student);Log.e("ObjectToJson",json);只有一句話 就完成了 簡單到爆有沒有 感謝馬云粑粑!!!

json轉對象
//將json轉為對象 參數1json 參數2對象類型 Student student=JSON.parseObject(json,Student.class); Log.e("JsonToObject","=========="+student.getName());同樣只有一句話 相對于android原生真是感人

list轉json
List<Student>stuList=new ArrayList<Student>();stuList.add(student);stuList.add(student);stuList.add(student);//List集合轉jsonjson=JSON.toJSONString(stuList);Log.e("ListToJson","=========="+json);集合中添加了三個同一個對象 json字符串的輸出 就變成了 ref,{0} 很明顯這是引用第一個對象 因為你添加了相同的對象 fastjson就不創建了 直接引用 這也是他號稱最快的原因
但是隨之而來的就有一個問題 fastjson識別引用 其他的jar不識別 如果服務器使用fastjson 客戶端使用gson 怎么辦嘞
1.都使用fastjson
2.在轉json的時候設置一條屬性 禁用循環引用對象 就ok
json=JSON.toJSONString(stuList,SerializerFeature.DisableCircularReferenceDetect);

json轉list
stuList=JSON.parseArray(json,Student.class); Student student1=stuList.get(0); Log.e("JsonToList","====================="+student1.getName());
有時候呢 并不需要對象里的所有字段 這時候就可以設置一個屬性過濾器 把你不需要的字段過濾掉
//過濾字段 屬性過濾器PropertyFilter json=JSON.toJSONString(stuList, new PropertyFilter() { @Override//參數1 正在被過濾的對象 參數2 過濾的屬性名 參數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);設置name過濾 請看log日志

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

把對象轉json

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

這時候就需要實現TypeReference類 把對象封裝一下

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

最后給大家介紹一個網站 http://json.cn/ 特別強大 會自動格式化json 如果有語法錯誤也會報錯滴
以上就是本文的全部內容,希望本文的內容對大家的學習或者工作能帶來一定的幫助,同時也希望多多支持武林網!
新聞熱點
疑難解答