8种常用json解析示例(原创作者:smallbee 本文转载于:http://smallbee.iteye.com/)
来源:互联网 发布:mac mini 加内存 编辑:程序博客网 时间:2024/05/28 22:12
- //转换器
- 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}*/
//转换器 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);
//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 {
- @Expose
- private String studentName;
- @Expose
- private 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 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 {
- @Expose
- private int id;
- @Expose
- private String name;
- @Expose
- private int age;
- @Expose
- private Student student;
- @Expose
- private List stus;
- @Expose
- private 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;
- }
- }
0 0
- 8种常用json解析示例(原创作者:smallbee 本文转载于:http://smallbee.iteye.com/)
- ios 推送消息 php做推送服务端(转载于http://zxs19861202.iteye.com/blog/1532460)
- Android中的JSON详细总结 本文转自:http://shazhuzhu1.iteye.com/blog/974758
- Spring 对静态资源的访问 转载本文地址:http://elf8848.iteye.com/blog/875830
- JSON解析 http://johncookie.iteye.com/blog/1140186
- Spring MVC 教程,快速入门,深入分析(转载自作者:赵磊 博客:http://elf8848.iteye.com)
- showModalDialog使用例子------转载于javaeye http://liyanhui.iteye.com/blog/253485
- 基于uml的需求分析与系统设计-转载于http://yunzhu.iteye.com
- mysql查看数据库和表的占用空间大小 作者:@狮子XL 本文为作者原创,转载请注明出处:http://www.cnblogs.com/iiiiher/p/5853133.html 目录 mysq
- SQL Server2000中死锁经验总结精彩讲述(本文转载于一个高手非原创)
- 安装protocolbuffer详解 原创: http://cq520.iteye.com/blog/2024739
- Android 源码编译(本文来源:http://quanminchaoren.iteye.com/blog/837333)
- 《大型网站技术架构》读书笔记 (转载请注明来源:http://zhanjia.iteye.com/blog/2038540)
- JNDI全面总结(转载自http://javacrazyer.iteye.com/blog/759485)
- IOS 设置启动图标 和 启动图片(转载自 http://justsee.iteye.com/blog/2123545)
- Java锁--synchronized(转载zi http://zhh9106.iteye.com/blog/2151791)
- mysql时间函数-- 转载http://laotu5i0.iteye.com/blog/691126
- cxf初步开发---转载http://tianjun309.iteye.com/blog/848776
- MongoDB基本命令用
- 数学 hdu 1719
- JAVASCRIPT学习总结
- Apple WatchKit 初探
- Javascript的"预编译"思考
- 8种常用json解析示例(原创作者:smallbee 本文转载于:http://smallbee.iteye.com/)
- c语言中的static变量和static函数
- <br>int rightFind(LIST_T *list, int row, int col, int isCircle, int *outRow, int *outCol)
- 黑马程序员——JAVA集合框架(二)
- var newExpression = resultSelector.Body as NewExpression;//转换为匿名对象表达式
- javascript基础知识归纳总结
- <br>int rightFind(LIST_T *list, int row, int col, int isCircle, int *outRow, int *outCol)
- var newExpression = resultSelector.Body as NewExpression;//转换为匿名对象表达式
- var newExpression = resultSelector.Body as NewExpression;//转换为匿名对象表达式