8种常用json解析示例(原创作者:smallbee 本文转载于:http://smallbee.iteye.com/)

2024-02-27 06:38

本文主要是介绍8种常用json解析示例(原创作者:smallbee 本文转载于:http://smallbee.iteye.com/),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

  1. //转换器  
  2.         GsonBuilder builder = new GsonBuilder();   
  3.         // 不转换没有 @Expose 注解的字段   
  4.         builder.excludeFieldsWithoutExposeAnnotation();  
  5.         Gson gson = builder.create();   
  6.           
  7.         //1、对象转string  
  8.         Student stu = new Student();  
  9.         stu.setStudentId(333);  
  10.         stu.setStudentName("qqq");  
  11.         String stuStr = gson.toJson(stu);  
  12.         System.out.println(stuStr); //{"studentName":"qqq","studentId":333}  
  13.           
  14.           
  15.         //2、string转对象  
  16.         Student user2 = gson.fromJson(stuStr, Student.class);   
  17.         System.out.println(user2);   
  18.         String stuTemp = "{\"studentName\":\"qqq2\",\"studentId\":3335}";  
  19.         Student user4 = gson.fromJson(stuTemp, Student.class);   
  20.         System.out.println(user4);   
  21.   
  22.         //3、对象List转string  
  23.         List<Student> testBeanList = new ArrayList<Student>();   
  24.         Student testBean = new Student();   
  25.         testBean.setStudentId(555);  
  26.         testBean.setStudentName("552");  
  27.         testBeanList.add(testBean);   
  28.           
  29.         //Gson gsonList = new Gson();   
  30.         Type type = new TypeToken<List<Student>>(){}.getType();  //指定集合对象属性  
  31.         String beanListToJson = gson.toJson(testBeanList, type);   
  32.         System.out.println(beanListToJson); //[{"studentName":"552","studentId":555}]   
  33.   
  34.         //集合string转对象list  
  35.         List<Student> testBeanListFromJson = gson.fromJson(beanListToJson, type);   
  36.         System.out.println(testBeanListFromJson); //[555:552]  
  37.   
  38.         //4、集合如果不指定类型 默认为String  
  39.         List<String> testList = new ArrayList<String>();   
  40.         testList.add("first");   
  41.         testList.add("second");   
  42.         String listToJson = gson.toJson(testList);   
  43.         System.out.println(listToJson); //["first","second"]   
  44.           
  45.         //5、集合字符串转回来需要指定类型  
  46.         List<String> testList2 = (List<String>) gson.fromJson(listToJson,   
  47.           new TypeToken<List<String>>() {   
  48.           }.getType());   
  49.         System.out.println(testList2);   
  50.   
  51.         //6、 将HashMap字符串转换为 JSON   
  52.         Map<String, String> testMap = new HashMap<String, String>();   
  53.         testMap.put("id""id.first");   
  54.         testMap.put("name""name.second");   
  55.         String mapToJson = gson.toJson(testMap);   
  56.         System.out.println(mapToJson); //{"id":"id.first","name":"name.second"}  
  57.         //7、stringMap转对象  
  58.         Map<String, String> userMap2 = (Map<String, String>) gson.fromJson(mapToJson,   
  59.                 new TypeToken<Map<String, String>>() {   
  60.             }.getType());   
  61.         System.out.println(userMap2); //{id=id.first, name=name.second}   
  62.   
  63.         //8、对象含有普通对象、集合、map情况  
  64.         Student user1 = new Student();   
  65.         user1.setStudentId(1001);   
  66.         user1.setStudentName("张三");   
  67.           
  68.         Student user3 = new Student();   
  69.         user3.setStudentId(1002);   
  70.         user3.setStudentName("李四");   
  71.           
  72.         Map<String, Student> userMap = new HashMap<String, Student>();   
  73.         userMap.put("user1", user1);   
  74.         userMap.put("user3", user3);   
  75.           
  76.         List<Student> userList = new ArrayList<Student>();   
  77.         userList.add(user1);   
  78.         userList.add(user3);   
  79.           
  80.         Teacher groupBean = new Teacher();   
  81.         groupBean.setStudent(user1);  
  82.         groupBean.setStus(userList);  
  83.         groupBean.setMap((HashMap)userMap);  
  84.         //groupBean.setUserList(userList);   
  85.         Gson gsonGroup = new Gson();   
  86.   
  87.         String sGroupBean = gsonGroup.toJson(groupBean, new TypeToken<Teacher>() {   
  88.             }.getType());   
  89.         System.out.println(sGroupBean);   
  90.         /*{"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、对象转stringStudent 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转stringList<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转对象listList<Student> testBeanListFromJson = gson.fromJson(beanListToJson, type); System.out.println(testBeanListFromJson); //[555:552]//4、集合如果不指定类型 默认为StringList<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}*/
Java代码   收藏代码
  1. //9、复杂对象string转对象  
  2. Teacher groupBean2 = (Teacher) gson.fromJson(sGroupBean,   
  3.    new TypeToken<Teacher>() {   
  4.    }.getType());   
  5. System.out.println(groupBean2);   
        //9、复杂对象string转对象Teacher groupBean2 = (Teacher) gson.fromJson(sGroupBean, new TypeToken<Teacher>() { }.getType()); System.out.println(groupBean2); 

 

Java代码   收藏代码
  1. package com.andtools;  
  2.   
  3. import com.google.gson.annotations.Expose;  
  4.   
  5. public class Student {  
  6.     @Expose  
  7.     private String studentName;  
  8.     @Expose  
  9.     private int studentId;  
  10.     public Student(){}  
  11.     public Student(int studentId,String studentName){  
  12.         this.setStudentId(studentId);  
  13.         this.setStudentName(studentName);  
  14.     }  
  15.     public String toString(){  
  16.         return this.getStudentId() + ":" + this.getStudentName();  
  17.     }  
  18.     public String getStudentName() {  
  19.         return studentName;  
  20.     }  
  21.     public void setStudentName(String studentName) {  
  22.         this.studentName = studentName;  
  23.     }  
  24.     public int getStudentId() {  
  25.         return studentId;  
  26.     }  
  27.     public void setStudentId(int studentId) {  
  28.         this.studentId = studentId;  
  29.     }  
  30.       
  31. }  
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;}}

 

Java代码   收藏代码
  1. package com.andtools;  
  2.   
  3. import java.util.HashMap;  
  4. import java.util.List;  
  5. import java.util.Map;  
  6.   
  7. import com.google.gson.annotations.Expose;  
  8.   
  9. public class Teacher {  
  10.     @Expose  
  11.     private int id;  
  12.     @Expose  
  13.     private String name;  
  14.     @Expose  
  15.     private int age;  
  16.     @Expose  
  17.     private Student student;  
  18.     @Expose  
  19.     private List stus;  
  20.     @Expose  
  21.     private HashMap map;  
  22.     public String toString(){  
  23.         return this.getId()+":"+this.getName()+":"+this.getAge() +":"this.getStudent().toString() + ":" + this.getStus() + ":" + this.getMap();  
  24.     }  
  25.     public int getId() {  
  26.         return id;  
  27.     }  
  28.     public void setId(int id) {  
  29.         this.id = id;  
  30.     }  
  31.     public String getName() {  
  32.         return name;  
  33.     }  
  34.     public void setName(String name) {  
  35.         this.name = name;  
  36.     }  
  37.     public int getAge() {  
  38.         return age;  
  39.     }  
  40.     public void setAge(int age) {  
  41.         this.age = age;  
  42.     }  
  43.     public Student getStudent() {  
  44.         return student;  
  45.     }  
  46.     public void setStudent(Student student) {  
  47.         this.student = student;  
  48.     }  
  49.     public List getStus() {  
  50.         return stus;  
  51.     }  
  52.     public void setStus(List stus) {  
  53.         this.stus = stus;  
  54.     }  
  55.     public HashMap getMap() {  
  56.         return map;  
  57.     }  
  58.     public void setMap(HashMap map) {  
  59.         this.map = map;  
  60.     }  
  61.       
  62. }  

这篇关于8种常用json解析示例(原创作者:smallbee 本文转载于:http://smallbee.iteye.com/)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/751510

相关文章

SQL 外键Foreign Key全解析

《SQL外键ForeignKey全解析》外键是数据库表中的一列(或一组列),用于​​建立两个表之间的关联关系​​,外键的值必须匹配另一个表的主键(PrimaryKey)或唯一约束(UniqueCo... 目录1. 什么是外键?​​ ​​​​2. 外键的语法​​​​3. 外键的约束行为​​​​4. 多列外键​

SpringBoot中HTTP连接池的配置与优化

《SpringBoot中HTTP连接池的配置与优化》这篇文章主要为大家详细介绍了SpringBoot中HTTP连接池的配置与优化的相关知识,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一... 目录一、HTTP连接池的核心价值二、Spring Boot集成方案方案1:Apache HttpCl

Java进行日期解析与格式化的实现代码

《Java进行日期解析与格式化的实现代码》使用Java搭配ApacheCommonsLang3和Natty库,可以实现灵活高效的日期解析与格式化,本文将通过相关示例为大家讲讲具体的实践操作,需要的可以... 目录一、背景二、依赖介绍1. Apache Commons Lang32. Natty三、核心实现代

Spring Boot 常用注解整理(最全收藏版)

《SpringBoot常用注解整理(最全收藏版)》本文系统整理了常用的Spring/SpringBoot注解,按照功能分类进行介绍,每个注解都会涵盖其含义、提供来源、应用场景以及代码示例,帮助开发... 目录Spring & Spring Boot 常用注解整理一、Spring Boot 核心注解二、Spr

使用Python自动化生成PPT并结合LLM生成内容的代码解析

《使用Python自动化生成PPT并结合LLM生成内容的代码解析》PowerPoint是常用的文档工具,但手动设计和排版耗时耗力,本文将展示如何通过Python自动化提取PPT样式并生成新PPT,同时... 目录核心代码解析1. 提取 PPT 样式到 jsON关键步骤:代码片段:2. 应用 JSON 样式到

Maven 插件配置分层架构深度解析

《Maven插件配置分层架构深度解析》:本文主要介绍Maven插件配置分层架构深度解析,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录Maven 插件配置分层架构深度解析引言:当构建逻辑遇上复杂配置第一章 Maven插件配置的三重境界1.1 插件配置的拓扑

MyBatisX逆向工程的实现示例

《MyBatisX逆向工程的实现示例》本文主要介绍了MyBatisX逆向工程的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学... 目录逆向工程准备好数据库、表安装MyBATisX插件项目连接数据库引入依赖pom.XML生成实体类、

Java中的内部类和常用类用法解读

《Java中的内部类和常用类用法解读》:本文主要介绍Java中的内部类和常用类用法,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录内部类和常用类内部类成员内部类静态内部类局部内部类匿名内部类常用类Object类包装类String类StringBuffer和Stri

$在R语言中的作用示例小结

《$在R语言中的作用示例小结》在R语言中,$是一个非常重要的操作符,主要用于访问对象的成员或组件,它的用途非常广泛,不仅限于数据框(dataframe),还可以用于列表(list)、环境(enviro... 目录1. 访问数据框(data frame)中的列2. 访问列表(list)中的元素3. 访问jav

MySQL连接池(Pool)常用方法详解

《MySQL连接池(Pool)常用方法详解》本文详细介绍了MySQL连接池的常用方法,包括创建连接池、核心方法连接对象的方法、连接池管理方法以及事务处理,同时,还提供了最佳实践和性能提示,帮助开发者构... 目录mysql 连接池 (Pool) 常用方法详解1. 创建连接池2. 核心方法2.1 pool.q