json-- google json 组件
来源:互联网 发布:apache网络服务器 编辑:程序博客网 时间:2024/06/07 17:16
package com.zmf.utils;
import java.util.List;
import java.util.Map;
import com.google.gson.ExclusionStrategy;
import com.google.gson.FieldAttributes;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonSyntaxException;
/**
* 处理json实用类
* 可以把实体类转换成json串
* 可以处理各种集合类,转换成相应的json格式
* 可以把对应的json格式转化成相应的集合类
* @author
*
*/
public class JsonUtils {
/**
* 内置json对象
*/
private JsonObject json = new JsonObject();
public synchronized JsonUtils put(Stringkey,String value){
json.addProperty(key,value);
returnthis;
}
public synchronized JsonUtils put(Stringkey,Character value){
json.addProperty(key,value);
returnthis;
}
public synchronized JsonUtils put(Stringkey,Number value){
json.addProperty(key,value);
returnthis;
}
public synchronized JsonUtils put(Stringkey,Boolean value){
json.addProperty(key,value);
returnthis;
}
public synchronized JsonUtils put(Stringkey,JsonElement value){
json.add(key,value);
returnthis;
}
public String toJsonString(){
return json.toString();
}
/**
* json 转对象
* @param json
* @param clazz
* @return T 返回类型
*/
public static <T> T json2Object(Stringjson,Class<T> clazz){
Gson gson = new GsonBuilder().create();
T obj = null;
try{
obj = gson.fromJson(json, clazz);
}catch(JsonSyntaxExceptione){
throwe;
}
returnobj;
}
/**
* 对象转json
* 包含expose注解的字段,空字段 都包含到json中
*
* @param obj
* @return
*/
public static String toJsonWithExposeAndNull(Objectobj){
GsonBuilder builder =new GsonBuilder();
builder.excludeFieldsWithoutExposeAnnotation();
builder.serializeNulls();
Gson gson = builder.create();
String jsonStr ="";
try{
jsonStr =gson.toJson(obj);
}catch(JsonSyntaxExceptione ){
throwe;
}
returnjsonStr;
}
/**
* 包含Expose注解的字段都被包含到json中
* @param obj
* @return
*/
public static String toJsonWithExpose(Objectobj){
GsonBuilder builder =new GsonBuilder();
builder.excludeFieldsWithoutExposeAnnotation();
Gson gson = builder.create();
String jsonStr = "";
try{
jsonStr =gson.toJson(obj);
}catch(JsonSyntaxExceptione){
throwe;
}
returnjsonStr;
}
/**
* 解决 putRootJson 数组为字符串的情况,有双引号,解析两边
* @param obj
* @return
*/
public static JsonElement toJsonTreeWithExpose(Objectobj){
Gson gson = new GsonBuilder().create();
JsonElement jsonElement =null;
try {
jsonElement =gson.toJsonTree(obj);
} catch (JsonSyntaxExceptione) {
throwe;
}
returnjsonElement;
}
/**
* @Description: TODO(json to list)
* @param @param jsonStr
* @param @param type
* @param @return 设定文件
* @return List<?> 返回类型
*/
public static <T> List<T> json2List(Stringjson, java.lang.reflect.Type type) {
Gson gson = new GsonBuilder().create();
List<T> objList =null;
try {
objList =gson.fromJson(json, type);
} catch (JsonSyntaxExceptione) {
throwe;
}
returnobjList;
}
/**
*
* @Title: jsonToMap
* @Description: 将json格式转换成map对象
* @param @param jsonStr
* @param @param type
* @param @return
* @return Map<K,V>
* @throws
*/
public static <K,V> Map<K,V> jsonToMap(StringjsonStr,java.lang.reflect.Type type){
Gson gson = new GsonBuilder().create();
Map<K,V> objMap =null;
try {
objMap =gson.fromJson(jsonStr, type);
}catch (JsonSyntaxExceptione) {
thrownew RuntimeException(e);
}
returnobjMap;
}
/**
* 将json格式转换成map对象
*
* @param jsonStr
* @return
*/
public static <K, V> Map<K, V> jsonToMap(Stringjson) {
Gson gson = new GsonBuilder().create();
Map<K,V> objMap =null;
try {
java.lang.reflect.Type type = new com.google.gson.reflect.TypeToken<Map<?, ?>>() {}.getType();
objMap =gson.fromJson(json, type);
}catch (Exceptione) {
thrownew RuntimeException(e);
}
returnobjMap;
}
/**
* 返回Json串,<b>默认包含实体类的所有值不为null的属性</b>
*
* @param entity
* @return
*/
public static String toJson(Objectentity) {
GsonBuilder builder =new GsonBuilder();
Gson gson = builder.create();
return gson.toJson(entity);
}
/**
* 返回Json串,<b>默认包含实体类的所有值不为null的属性</b>
*
* @param entity
* 要传入的实体类对象
* @param clazzes
* 不需要转化的实体类
* @return json串
*
*/
public static String toJson(Objectentity, final Class<?>... clazzes) {
GsonBuilder builder =new GsonBuilder();
// 设置除外的属性名
builder.setExclusionStrategies(new ExclusionStrategy() {
/*
* 设置要过滤的类
*/
publicboolean shouldSkipClass(Class<?> clazz) {
booleanflag = false;
// 如果返回true就表示此类被忽略
if (clazzes !=null) {
for (Class<?>c : clazzes) {
if (clazz ==c) {
flag =true;
}
break;
}
}
returnflag;
}
/**
* 跳过的字段
*/
publicboolean shouldSkipField(FieldAttributes attribute) {
returnfalse;
}
});
Gson gson = builder.create();
return gson.toJson(entity);
}
/**
* 返回Json串,<b>默认包含实体类的所有值不为null的属性</b>
*
* @param entity
* 要传入的实体类
* @param excludeNames
* 不包括的字段
* @param clazzes
* 不需要转化的实体类
* @return
*/
public static String toJson(final Objectentity,
final List<String>excludeNames, final Class<?>... clazzes) {
GsonBuilder builder =new GsonBuilder();
// 设置除外的属性名
builder.setExclusionStrategies(new ExclusionStrategy() {
/*
* 设置要过滤的类
*/
publicboolean shouldSkipClass(Class<?> clazz) {
booleanflag = false;
// 如果返回true就表示此类被忽略
if (clazzes !=null) {
for (Class<?>c : clazzes) {
if (clazz ==c) {
flag =true;
}
break;
}
}
returnflag;
}
/**
* 跳过的字段
*/
publicboolean shouldSkipField(FieldAttributes attribute) {
booleanflag = false;
if (excludeNames !=null) {
flag =excludeNames.contains(attribute.getName());
}
returnflag;
}
});
Gson gson = builder.create();
return gson.toJson(entity);
}
/**
* 返回Json串,<b>默认包含实体类的所有属性,包括值为null的属性</b>
*
* @return json串
*/
public static String toJsonIncludeNull(Objectentity) {
GsonBuilder builder =new GsonBuilder();
builder.serializeNulls();
Gson gson = builder.create();
return gson.toJson(entity);
}
/**
* 返回Json串,<b>默认包含实体类的所有属性,包括值为null的属性</b>
*
* @param clazzes
* 不需要转化的实体类
* @return json串
*/
public static String toJsonIncludeNull(Objectentity,
final Class<?>...clazzes) {
GsonBuilder builder =new GsonBuilder();
builder.serializeNulls();
// 设置除外的属性名
builder.setExclusionStrategies(new ExclusionStrategy() {
/*
* 设置要过滤的类
*/
publicboolean shouldSkipClass(Class<?> clazz) {
booleanflag = false;
// 如果返回true就表示此类被忽略
if (clazzes !=null) {
for (Class<?>c : clazzes) {
if (clazz ==c) {
flag =true;
}
break;
}
}
returnflag;
}
/**
* 跳过的字段
*/
publicboolean shouldSkipField(FieldAttributes attribute) {
returnfalse;
}
});
Gson gson = builder.create();
return gson.toJson(entity);
}
/**
* 返回Json串,<b>默认包含实体类的为null的属性</b>
*
* @param entity
* 要传入的实体类
* @param excludeNames
* 不包括的字段
* @return
*/
public static String toJsonIncludeNull(final Objectentity,
final List<String>excludeNames, final Class<?>... clazzes) {
GsonBuilder builder =new GsonBuilder();
builder.serializeNulls();
// 设置除外的属性名
builder.setExclusionStrategies(new ExclusionStrategy() {
/*
* 设置要过滤的类
*/
publicboolean shouldSkipClass(Class<?> clazz) {
booleanflag = false;
// 如果返回true就表示此类被忽略
if (clazzes !=null) {
for (Class<?>c : clazzes) {
if (clazz ==c) {
flag =true;
}
break;
}
}
returnflag;
}
/**
* 跳过的字段
*/
publicboolean shouldSkipField(FieldAttributes attribute) {
booleanflag = false;
if (excludeNames !=null) {
flag =excludeNames.contains(attribute.getName());
}
returnflag;
}
});
Gson gson = builder.create();
return gson.toJson(entity);
}
/**
* 根据传入的json串,构造实体类对象
*
* @param <T>
* @param json
* 传入的json字符串
* @param clazz
* 传入的实体类类型
* @return
*/
public static <T> T fromJson(Stringjson, Class<T> clazz) {
GsonBuilder builder =new GsonBuilder();
Gson gson = builder.create();
return gson.fromJson(json, clazz);
}
/**
* 返回Gson对象
* @return
*/
public static Gson getGson(){
return new GsonBuilder().create();
}
/**
* 把字符串转化成相应的JsonElement对象
*
* @param jsonData
* @return
*/
public static JsonElement parse(StringjsonData) {
JsonParser jsonParser =new JsonParser();
returnjsonParser.parse(jsonData);
}
}
- json-- google json 组件
- google-json
- Gson google json使用
- google json解析
- JSON in google Maps
- json数据转换组件
- google json API--json to java ojbect
- google json和sf.json的区别
- Google浏览器Json插件(JSON-handle)
- .net json序列化组件Json.NET(Newtonsoft.Json)
- Google Translate And Json Parsing - Google翻译与Json解析
- JSON and Google Local Search
- Google Suggest(Ajax+JSON+Servlet)
- Json 解析之google.gson
- 第三方类库-JSON-Google Gson
- JSON-RPC组件的使用
- Json
- JSON
- Python爬虫入门:Urllib库的基本使用
- jieba中文处理
- Eclipse中将输出信息显示到控制台同时存到指定日志中
- c实现signal函数实例
- Oracle(三)jdbc,系统函数,事物,约束,sequence,index
- json-- google json 组件
- Java与安卓PDF书籍
- IDEA安装及总结
- CSDN-markdown 首行缩进的快捷实现
- 实战案例——小帅优化网站,70天目标关键词全部第一
- Noip 2003 加分二叉树
- java解析xml文档的几种方式,概论
- C++ 程序崩溃调用堆栈打印
- 高端智能冰箱成白电市场新宠,营造品质生活方式为上策