虽然android自带了json处理,但是没有封装,总觉得不方便,网上找了gson的处理,觉得还行。
//转换器        GsonBuilder builder = new GsonBuilder();         // 不转换没有 @Expose 注解的字段         builder.excludeFieldsWithoutExposeAnnotation();        Gson gson = builder.create();                 //1、对象转string        Student stu = new Student();        stu.setStudentId(333);        stu.setStudentName("qqq");        String stuStr = gson.toJson(stu);        System.out.println(stuStr); //{"studentName":"qqq","studentId":333}                        //2、string转对象        Student user2 = gson.fromJson(stuStr, Student.class);         System.out.println(user2);         String stuTemp = "{\"studentName\":\"qqq2\",\"studentId\":3335}";        Student user4 = gson.fromJson(stuTemp, Student.class);         System.out.println(user4);         //3、对象List转string        List<Student> testBeanList = new ArrayList<Student>();         Student testBean = new Student();         testBean.setStudentId(555);        testBean.setStudentName("552");        testBeanList.add(testBean);                 //Gson gsonList = new Gson();         Type type = new TypeToken<List<Student>>(){}.getType();  //指定集合对象属性        String beanListToJson = gson.toJson(testBeanList, type);         System.out.println(beanListToJson); //[{"studentName":"552","studentId":555}]         //集合string转对象list        List<Student> testBeanListFromJson = gson.fromJson(beanListToJson, type);         System.out.println(testBeanListFromJson); //[555:552]        //4、集合如果不指定类型 默认为String        List<String> testList = new ArrayList<String>();         testList.add("first");         testList.add("second");         String listToJson = gson.toJson(testList);         System.out.println(listToJson); //["first","second"]                 //5、集合字符串转回来需要指定类型        List<String> testList2 = (List<String>) gson.fromJson(listToJson,           new TypeToken<List<String>>() {           }.getType());         System.out.println(testList2);         //6、 将HashMap字符串转换为 JSON         Map<String, String> testMap = new HashMap<String, String>();         testMap.put("id", "id.first");         testMap.put("name", "name.second");         String mapToJson = gson.toJson(testMap);         System.out.println(mapToJson); //{"id":"id.first","name":"name.second"}        //7、stringMap转对象        Map<String, String> userMap2 = (Map<String, String>) gson.fromJson(mapToJson,         new TypeToken<Map<String, String>>() {     }.getType());         System.out.println(userMap2); //{id=id.first, name=name.second}         //8、对象含有普通对象、集合、map情况        Student user1 = new Student();         user1.setStudentId(1001);         user1.setStudentName("张三");                 Student user3 = new Student();         user3.setStudentId(1002);         user3.setStudentName("李四");                 Map<String, Student> userMap = new HashMap<String, Student>();         userMap.put("user1", user1);         userMap.put("user3", user3);                 List<Student> userList = new ArrayList<Student>();         userList.add(user1);         userList.add(user3);                 Teacher groupBean = new Teacher();         groupBean.setStudent(user1);        groupBean.setStus(userList);        groupBean.setMap((HashMap)userMap);        //groupBean.setUserList(userList);         Gson gsonGroup = new Gson();         String sGroupBean = gsonGroup.toJson(groupBean, new TypeToken<Teacher>() {         }.getType());         System.out.println(sGroupBean);         /*{"stus":[{"studentName":"张三","studentId":1001},{"studentName":"李四","studentId":1002}],"student":{"studentName":"张三","studentId":1001},"map":{"user3":{"studentName":"李四","studentId":1002},"user1":{"studentName":"张三","studentId":1001}},"id":0,"age":0}*/
        //9、复杂对象string转对象        Teacher groupBean2 = (Teacher) gson.fromJson(sGroupBean,           new TypeToken<Teacher>() {           }.getType());         System.out.println(groupBean2); 

package com.andtools;import com.google.gson.annotations.Expose;public class Student {@Exposeprivate String studentName;@Exposeprivate int studentId;public Student(){}public Student(int studentId,String studentName){this.setStudentId(studentId);this.setStudentName(studentName);}public String toString(){return this.getStudentId() + ":" + this.getStudentName();}public String getStudentName() {return studentName;}public void setStudentName(String studentName) {this.studentName = studentName;}public int getStudentId() {return studentId;}public void setStudentId(int studentId) {this.studentId = studentId;}}

package com.andtools;import java.util.HashMap;import java.util.List;import java.util.Map;import com.google.gson.annotations.Expose;public class Teacher {@Exposeprivate int id;@Exposeprivate String name;@Exposeprivate int age;@Exposeprivate Student student;@Exposeprivate List stus;@Exposeprivate HashMap map;public String toString(){return this.getId()+":"+this.getName()+":"+this.getAge() +":"+ this.getStudent().toString() + ":" + this.getStus() + ":" + this.getMap();}public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public Student getStudent() {return student;}public void setStudent(Student student) {this.student = student;}public List getStus() {return stus;}public void setStus(List stus) {this.stus = stus;}public HashMap getMap() {return map;}public void setMap(HashMap map) {this.map = map;}}

附件为网上找的文档。

更多相关文章

  1. 类和 Json对象
  2. Android(安卓)Studio修改apk命名
  3. 自定义ProgressBar的样式失效
  4. android framework在launcher中隐藏指定app
  5. android SQLite数据库基本操作示例
  6. android 蓝牙通讯
  7. android文件下载
  8. android发送json并解析返回json
  9. Android(安卓)问题集合

随机推荐

  1. 饺子播放器、IjkVideoView播放器的简单使
  2. Android(安卓)-- PullToRefresh应用
  3. Android六大优势
  4. Android流媒体
  5. Android布局优化之TextView、ImageView合
  6. Android中native进程内存泄露的调试技巧
  7. 别再问我Android前景如何
  8. android 中一个工程引用另一个工程
  9. Android中网络编程以及与服务器上Web项目
  10. Android(安卓)SDK Manager更新 != 问题