Gson手动序列化POJO(工具类)
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(工具类)的更多相关文章
- Java 序列化对象工具类
SerializationUtils.java package javax.utils; import java.io.ByteArrayInputStream; import java.io.Byt ...
- 记录--Gson、json转实体类、类转json
需要导入Gson jar包 最近在做一个java web service项目,需要用到jason,本人对java不是特别精通,于是开始搜索一些java平台的json类库. 发现了google的gson ...
- json工具类(二)——google包
package com.ruoyi.common.utils.json; import java.util.List; import com.google.gson.Gson; import com. ...
- Java常用工具类整理
字符数组转String package com.sunsheen.hcc.fabric.utils; /** * 字符数组工具 * @author WangSong * */ public class ...
- Properties-转换流-打印流-序列化和反序列化-Commons-IO工具类
一.Properties 类(java.util) 概述:Properties 是一个双列集合;Properties 属于map的特殊的孙子类;Properties 类没有泛型,propert ...
- Gson/Jackson/FastJson工具类
import java.util.ArrayList; import java.util.List; import java.util.Map; import com.google.gson.Gson ...
- 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 ...
- redis缓存工具类,提供序列化接口
1.序列化工具类 package com.qicheshetuan.backend.util; import java.io.ByteArrayInputStream; import java.io. ...
- Json转换工具类(基于google的Gson和阿里的fastjson)
在项目之中我们经常会涉及到字符串和各种对象的转换,为此特地整理了一下常用的转换方法 一.基于com.google.code.gson封装的json转换工具类 1. 在pom.xml文件里面引入gson ...
随机推荐
- java 向上转型 向下转型
//父类 四边形 class Quadrangle{ public static void draw (Quadrangle q){ } } //子类 public class Parallelog ...
- C#调用杀毒软件MSE扫描指定目录或文件
有这样的需求,对外网文件传到服务器上时,对文件扫描是否含有病毒.微软自己的杀毒软件MSE实现了提供了命令行调用,方便我们集成到C#开发的程序里面. 命令如下: -file "E:\t&quo ...
- js 点赞数 处理
likeNum(num) { if (num === 0) { num = ''; } else if (num > 9999 && num <= 9999999) { n ...
- Java回顾之Spring基础
第一篇:Java回顾之I/O 第二篇:Java回顾之网络通信 第三篇:Java回顾之多线程 第四篇:Java回顾之多线程同步 第五篇:Java回顾之集合 第六篇:Java回顾之序列化 第七篇:Java ...
- C# 程序中的变量
变量命名规则: 不能是c#关键字 由字母,数字,下划线构成 第一个不能是数字 不要超过31个字符 不能是函数名,类名 c#是大小写敏感的. 本质上,数据类型就是他存储方式和他参与运算的抽象. c#的数 ...
- javascript的语法结构
字符规范: javascript程序是采用的Unicode字符集编写的,并且区分大小写.但是html代码不区分大小写,比如,在html中点击事件就可以写成onClick或则onclick,但是在jav ...
- SQL2005中的事务与锁定(五)- 转载
------------------------------------------------------------------------ -- Author : HappyFlyStone - ...
- animation 的属性一共有 6 个值,详细介绍在此
animation 属性是一个简写属性,用于设置六个动画属性: animation-name animation-duration animation-timing-function animatio ...
- * 和 ** python
*代表tuple集合,**代表dict def func(a, b, c=0, *args, **kw) print ('a=',a, 'b=',b,'c=',c,'args=',args,'kw= ...
- CSS 笔记五(Combinators/Pseudo-classes/Pseudo-elements)
CSS Combinators Four different combinators in CSS3 descendant selector (space) child selector (>) ...