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. 使用NSURLSession

    NSURLConnection在iOS9被宣布弃用,NSURLSession从13年发展到现在,终于迎来了它独步江湖的时代.NSURLSession是苹果在iOS7后为HTTP数据传输提供的一系列接口 ...

  2. DuiLib学习笔记1——编译运行demo

    c++中皮肤问题比较麻烦,MFC自带的太难用.DirectUI界面库就比较强大了,之前像skin++之类的基于DirectUI收费昂贵.DuiLib是基于DirectUI的界面库,可以将用户界面和处理 ...

  3. adop - ERRORMSG: Since earlier patching session failed and you are invoking apply again

    $ adop phase=apply patches= hotpatch=yes *******FATAL ERROR******* PROGRAM : (/app/oracle/apps/VIS/f ...

  4. python install (version problem-method ln -s)

    一般情况下,无需自己安装Python.因为在大多数Linux版本中,如Fedora,Ubuntu等,都已经默认安装了Python,但也可以根据需要自定义安装Python.下面使用源码编译安装来举个例子 ...

  5. Spark+Hadoop+Hive集群上数据操作记录

    [rc@vq18ptkh01 ~]$ hadoop fs -ls / drwxr-xr-x+ - jc_rc supergroup 0 2016-11-03 11:46 /dt [rc@vq18ptk ...

  6. 20145320《Java程序设计》第五次实验报告

    20145320<Java程序设计>第五次实验报告 北京电子科技学院(BESTI)实验报告 课程:Java程序设计 班级:1453 指导教师:娄嘉鹏 实验日期:2016.05.06 18: ...

  7. 关于Block的copy和循环引用的问题

    http://blog.csdn.net/felix9/article/details/9619313 在实际开发中,发现使用Block有着比delegate和notification更简洁的优势.于 ...

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

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

  9. php中rsa加密及解密和签名及验签

    加密的内容长度限制为密钥长度少位,如位的密钥最多加密的内容为个长度. 公钥加密 $public_content=file_get_contents(公钥路径); $public_key=openssl ...

  10. 初识python第二天(2)

    整理Python常见数据类型内置函数的使用方法如下: 一.int 首先我们来查看一下int包含了哪些函数 #python3.x print(dir(int)) #['__abs__', '__add_ ...