gson2.7版本

只是简单的工具类(练习所用):

package pojo;
import javax.xml.bind.annotation.XmlSeeAlso; import com.google.gson.annotations.SerializedName; public class User {
public String name;
public int age;
@SerializedName(value="email_Address",alternate={"email"})
public String emailAddress;
public User() {
}
public User(String name, int age, String emailAddress) {
super();
this.name = name;
this.age = age;
this.emailAddress = emailAddress;
}
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 String getEmailAddress() {
return emailAddress;
}
public void setEmailAddress(String emailAddress) {
this.emailAddress = emailAddress;
}
@Override
public String toString() {
return "User [name=" + name + ", age=" + age + ", emailAddress=" + emailAddress + "]";
} }
package utils;

import java.io.IOException;
import java.io.StringReader;
import java.lang.reflect.Field; import com.google.gson.Gson;
import com.google.gson.stream.JsonReader; import pojo.User; /**
* 处理json工具类
* @Description:
* @author zbs
* @date 2016年9月30日 上午10:06:05
*/
public class JsonUtils {
public static void main(String[] args) {
String json="{\"name\":\"学学习\",\"age\":\"24\"}";
User user = (User) getObjectFromJson(User.class, json);
System.out.println(user);
}
/**
* json-->pojo
* @Description:序列化json
* @param clazz
* @param json
* @return void 返回类型
*/
@SuppressWarnings("all")
public static Object getObjectFromJson(Class clazz, String json) {
JsonReader reader = new JsonReader(new StringReader(json));
Field[] field = clazz.getDeclaredFields();
Object obj = null;
try {
// 获取当前对象
obj = clazz.newInstance();
reader.beginObject();
while (reader.hasNext()) {
String name = reader.nextName();
for (Field f : field) {
if (f.getName().equals(name)) {
if (f.getType() == int.class) {
f.setInt(obj, reader.nextInt());
}else if(f.getType() == Double.class){
f.setDouble(obj, reader.nextDouble());
}else if (f.getType() == Long.class) {
f.setLong(obj, reader.nextLong());
}else if (f.getType() == Boolean.class) {
f.setBoolean(obj, reader.nextBoolean());
}else{
f.set(obj, reader.nextString());
}
}
}
}
reader.endObject();
} catch (Exception e) {
e.printStackTrace();
}
return obj;
}
}

  

  

Gson手动序列化POJO(工具类)的更多相关文章

  1. Java 序列化对象工具类

    SerializationUtils.java package javax.utils; import java.io.ByteArrayInputStream; import java.io.Byt ...

  2. 记录--Gson、json转实体类、类转json

    需要导入Gson jar包 最近在做一个java web service项目,需要用到jason,本人对java不是特别精通,于是开始搜索一些java平台的json类库. 发现了google的gson ...

  3. json工具类(二)——google包

    package com.ruoyi.common.utils.json; import java.util.List; import com.google.gson.Gson; import com. ...

  4. Java常用工具类整理

    字符数组转String package com.sunsheen.hcc.fabric.utils; /** * 字符数组工具 * @author WangSong * */ public class ...

  5. Properties-转换流-打印流-序列化和反序列化-Commons-IO工具类

    一.Properties 类(java.util)     概述:Properties 是一个双列集合;Properties 属于map的特殊的孙子类;Properties 类没有泛型,propert ...

  6. Gson/Jackson/FastJson工具类

    import java.util.ArrayList; import java.util.List; import java.util.Map; import com.google.gson.Gson ...

  7. c#中@标志的作用 C#通过序列化实现深表复制 细说并发编程-TPL 大数据量下DataTable To List效率对比 【转载】C#工具类:实现文件操作File的工具类 异步多线程 Async .net 多线程 Thread ThreadPool Task .Net 反射学习

    c#中@标志的作用   参考微软官方文档-特殊字符@,地址 https://docs.microsoft.com/zh-cn/dotnet/csharp/language-reference/toke ...

  8. redis缓存工具类,提供序列化接口

    1.序列化工具类 package com.qicheshetuan.backend.util; import java.io.ByteArrayInputStream; import java.io. ...

  9. Json转换工具类(基于google的Gson和阿里的fastjson)

    在项目之中我们经常会涉及到字符串和各种对象的转换,为此特地整理了一下常用的转换方法 一.基于com.google.code.gson封装的json转换工具类 1. 在pom.xml文件里面引入gson ...

随机推荐

  1. Sublime Text 3 3126 注册码

    转载自:https://fatesinger.com/78252 Sublime Text 3 3126 注册码 第一个测试通过 -– BEGIN LICENSE -– Michael Barnes ...

  2. Python开发程序:简单主机批量管理工具

    题目:简单主机批量管理工具 需求: 主机分组 登录后显示主机分组,选择分组后查看主机列表 可批量执行命令.发送文件,结果实时返回 主机用户名密码可以不同 流程图: 说明: ### 作者介绍: * au ...

  3. IE localhost 不能解析

    新建的项目  在虚拟机里试了,虚拟机的IE可以解析.本机的360.谷歌都可以解析 只有IE不可以,我把IE11卸载了换成IE8也不行.再换回IE11还是不行 在网上找了很多方法 最后  看到有一个人 ...

  4. 打开VS调试不进入开发的网站直接跳转到主页

    重启了熟悉有卸载IE11的,搞了好几个小时 最后把电脑管家里的锁定主页打开就好了! 很久之后  我再锁上  也没有这问题了

  5. leetcode 257

    查找二叉树中根节点到叶子节点的所有路径: 本题有两种解法:递归解法和非递归解法,递归解法请参考:http://blog.csdn.net/booirror/article/details/477331 ...

  6. Spring MVC 入门示例讲解

    在本例中,我们将使用Spring MVC框架构建一个入门级web应用程序.Spring MVC 是Spring框架最重要的的模块之一.它以强大的Spring IoC容器为基础,并充分利用容器的特性来简 ...

  7. jQuery选择器和事件

    选择器 常用事件 绑定与解除绑定 事件目标与冒泡 自定义事件

  8. java mybatis 动态sql

    //-------------------------------查询-------------------------------------// <sql id="cmsGuest ...

  9. ThinkPHP讲解(十一)——验证码和文件上传

    一.验证码 1.页面前端显示 (验证码是图片标签,来源是控制器里的yzm()操作方法) <h1>登录</h1> <form action="__ACTION__ ...

  10. 记录一下我使用的vim的配置文件

    还不是很完美: "au BufReadPost * if line("'\"") > 0|if line("'\"") &l ...