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. IntelliJ IDEA全文搜索很给力

  2. 简单理解php的socket编程

    php的socket编程算是比较难以理解的东西吧,不过,我们只要理解socket几个函数之间的关系,以及它们所扮演的角色,那么理解起来应该不是很难了,在笔者看来,socket编程,其实就是建立一个网络 ...

  3. ionic下拉加载自动触发

    ionic提供的下拉加载,是要滑动去下拉加载,没有提供api自动触发下拉加载,比如刚进页面,或者切换tab时想触发一次下拉加载. 添加如下service: angular.module('YourAp ...

  4. 深入SpringBoot:自定义Endpoint

    前言 上一篇文章介绍了SpringBoot的PropertySourceLoader,自定义了Json格式的配置文件加载.这里再介绍下EndPoint,并通过自定EndPoint来介绍实现原理. En ...

  5. xavante运行cgilua流程

    安装软件 需要安装三个lua 库:  xavante wsapi cgilua luarocks install xavante http://keplerproject.github.io/xava ...

  6. JAVA:NIO初步了解

    简介: Java NIO(New IO)是一个可以替代标准Java IO API的IO API(从Java 1.4开始),Java NIO提供了与标准IO不同的IO工作方式. Java NIO: Ch ...

  7. 使用 robotframework 自动化测试系列 一 -----简介

    robotframework 是自动化测试框架. Robot Framework是一款python编写的功能自动化测试框架.具备良好的可扩展性,支持关键字驱动,可以同时测试多种类型的客户端或者接口,可 ...

  8. 关于python文件操作

    http://www.cnblogs.com/rollenholt/archive/2012/04/23/2466179.html 总是记不住API.昨晚写的时候用到了这些,但是没记住,于是就索性整理 ...

  9. 微信开发那点事(No.001)

    MXS&Vincene  ─╄OvЁ  &0000024─╄OvЁ  MXS&Vincene MXS&Vincene  ─╄OvЁ:今天很残酷,明天更残酷,后天很美好, ...

  10. html5 Web Storage(localStorage(),sessionStorage())

    Web Storage包括了两种存储方式:sessionStorage和localStorage sessionStorage 是会话级别的存储,这些数据只有在同一个会话中的页面才能访问并且当会话结束 ...