Android开发_Gson解析
- //转换器
- GsonBuilder builder = new GsonBuilder();
- // 不转换没有 @Expose 注解的字段
- builder.excludeFieldsWithoutExposeAnnotation();
- Gson gson = builder.create();
- //1、对象转string
- Student stu = new Student();
- );
- 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.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.setStudentName("张三");
- Student user3 = new Student();
- );
- 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 {
- @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 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;
- }
- }
Android开发_Gson解析的更多相关文章
- Android开发pool解析xml
xml在开发中的作用不可小觑,很多时候我们都要用到这种文件,所以学习它的解析方式很是必要. 我们都知道java中xml的解析有:dom,SAX,但是Android下我们使用pool解析,是更为方便,而 ...
- Android开发AlertDialog解析
打开源码,首先映入眼帘的是三个构造方法,但这三个构造方法都是protected类型的, 可见,不允许我们直接实例化AlertDialog. 因此,我们再看别的有没有方法.可以实例化 再仔细一看,发现一 ...
- Android开发MVP模式解析
http://www.cnblogs.com/bravestarrhu/archive/2012/05/02/2479461.html 在开发Android应用时,相信很多同学遇到和我一样的情况,虽然 ...
- Android开发:碎片Fragment完全解析fragment_main.xml/activity_main.xml
Android开发:碎片Fragment完全解析 为了让界面可以在平板上更好地展示,Android在3.0版本引入了Fragment(碎片)功能,它非常类似于Activity,可以像 Activi ...
- [android开发IDE]adt-bundle-windows-x86的一个bug:无法解析.rs文件--------rs_core.rsh file not found
google的android自带的apps写的是相当牛逼的,将其导入到eclipse中方便我们学习扩展.可惜关于导入的资料太少了,尤其是4.1之后的gallery和camera合二为一了.之前导4.0 ...
- Android开发周报:Flyme OS开源、经典开源项目解析
Android开发周报:Flyme OS开源.经典开源项目解析 新闻 <魅族Flyme OS源码上线Github> :近日魅族正式发布了MX5,并且在发布会上,魅族还宣布Flyme OS开 ...
- 【Android开发精要笔记】Android组件模型解析
Android组件模型解析 Android中的Mashup 将应用切分成不同类别的组件,通过统一的定位模型和接口标准将他们整合在一起,来共同完成某项任务.在Android的Mashup模式下,每个组件 ...
- 【Android开发日记】之入门篇(十三)——Android的控件解析
Android的控件都派生自android.view.View类,在android.widget包中定义了大量的系统控件供开发者使用,开发者也可以从View类及其子类中,派生出自定义的控件. 一.An ...
- 《ArcGIS Runtime SDK for Android开发笔记》——(6)、基于Android Studio的ArcGIS Android工程结构解析
1.前言 Android Studio 是第一个Google官方的 Android 开发环境.其他工具,例如 Eclipse,在 Android Studio 发布之前已经有了大规模的使用.为了帮助开 ...
随机推荐
- 转发:maven打包时始终出现以下提示:-source 1.3 中不支持泛型(请使用 -source 5 或更高版本以启用泛型)
maven打包时始终出现以下提示: 1.-source 1.3 中不支持泛型(请使用 -source 5 或更高版本以启用泛型)List<User> userList= new Array ...
- UVALive 6602 Counting Lattice Squares
给定一个n*m的网格,求面积为奇数的正方形有多少个. 首先是n*m个面积为1的,然后剩下的要么是边长为奇数,要么被这样一个奇数边长所包围. 原因如下: 对于一个边长不平行于坐标抽的正方形,其边长一定是 ...
- *[topcoder]HexagonalBoard
http://community.topcoder.com/stat?c=problem_statement&pm=12784 真心觉得tc的div1 250不少好题,对我来说比较适合.这道题 ...
- Altium Designer 常用快捷键总结
原理图:1:按住shift 拖动某个元件,可快速复制.2:按住鼠标滚轮 鼠标上下滑动 放大缩小.3:按住Ctrl 按住鼠标右键 鼠标上下滑动也放大缩小.4:按住Ctrl 拖动某个元件 可以移动位置 ...
- 6个常见的 PHP 安全性攻击
了解常见的PHP应用程序安全威胁,可以确保你的PHP应用程序不受攻击.因此,本文将列出 6个常见的 PHP 安全性攻击,欢迎大家来阅读和学习. 1.SQL注入 SQL注入是一种恶意攻击,用户利用在表单 ...
- hdu4648Magic Pen 6
http://acm.hdu.edu.cn/showproblem.php?pid=4648 求连续的一段和对m取余为0 若s[j]和s[i]对M的余数都相同 则相见就满足要求 找个最长的 #inc ...
- MVC 自定义AuthorizeAttribute实现权限管理
在上一节中提到可以使用AuthorizeAttribute进行权限管理: [Authorize] public ActionResult TestAuthorize() { return View() ...
- HDU3047 Zjnu Stadium 带权并查集
转:http://blog.csdn.net/shuangde800/article/details/7983965 #include <cstdio> #include <cstr ...
- asp.net 中Session的运用,及抛出错误“未将对象引用设置到对象的实例”
1. 页面载入后,必须要等到page_Load方法执行建立 page对象后才可以使用Session 2. 在.aspx和.cs文件中使用Session的区别 (1).aspx: Session[&qu ...
- 安装solr
安装之前先关闭防火墙.安装好jdk和tomcat 1.启动tomcat的命令为:apache-tomcat-7.0.61/bin/startup.sh 2.拷贝solr目录下example/webap ...