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. JS之对象

    每个对象的属性有两种,每种属性有4中特征描述符 1.数据属性 1.1 [[configurable]]:表示不能通过delete删除属性,不能修改属性的特性,不能将数据属性改为访问器属性,默认值fal ...

  2. 《Android 性能测试初探》

    移动测试站点推荐: https://testerhome.com/ 专项相关帖子推荐: <Android 性能测试初探>合集 移动无线应用专项测试浅谈 公开课: [腾讯课堂]Testerh ...

  3. [转]C++11 多线程

    转载自:http://www.cnblogs.com/zhuyp1015/archive/2012/04/08/2438288.html C++11开始支持多线程编程,之前多线程编程都需要系统的支持, ...

  4. IOS asc码替换

    -(void)click:(UIButton *)btn { NSString *testStr = @"学>与全等"; NSStringEncoding encoding2 ...

  5. Number类型方法

    //1.toString();  转换成字符串 var s=123; console.log(typeof s.toString()); //string //2.toLocaleString()   ...

  6. 配置MyBatis

    创建WebProject之后先把mybatis-3.2.2.zip里的mybatis-3.2.2.jar包和lib里的asm-3.3.1.jar.cglib-2.2.2.jar.commons-log ...

  7. php递归无限极分类实例

    无限级分类原理简介 无限分类看似"高大上",实际上原理是非常简单的 .无限分类不仅仅需要代码的巧妙性,也要依托数据库设计的合理性.要满足无限级分类,数据库需要有两个必须的字段,id ...

  8. How to use usercontrol - pass paramters

    在线PhotoShop http://uupoop.com/ In the previous chapter we created a UserControl, and now we will try ...

  9. Centos6.7安装docker1.7.1

    Docker当前发布的最新版本已经到了1.11,其官网上针对Centos的的安装需求如下: Docker requires a -bit installation regardless of your ...

  10. spring环境搭建需要的插件-------Spring Tool Suite™ Downloads

    下载地址http://spring.io/tools/sts/all 上面的是集成了eclipse的,所以文件比较大,下面的是单独的插件,下载之后打开eclipse,help->installN ...