自己封装的json工具类
package com.develop.util; import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map; import net.sf.ezmorph.object.DateMorpher;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;
import net.sf.json.JsonConfig;
import net.sf.json.util.CycleDetectionStrategy;
import net.sf.json.util.JSONUtils;
import net.sf.json.util.PropertyFilter; public class JsonUtil {
/**
* 转成jsonOjbect对象
* @param obj
* @return
*/
public static JSONObject toJsonOjbect(Object obj){
JsonConfig jsonConfig = new JsonConfig();
jsonConfig.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);//可防止hibernate模式下的关联关系子对象中包含父对象造成死循环
JSONObject jsonObject = JSONObject.fromObject(obj,jsonConfig);
return jsonObject;
} /**
* 转成jsonArray对象
* @param obj
* @return
*/
public static JSONArray toJsonArray(Object obj){
JsonConfig jsonConfig = new JsonConfig();
jsonConfig.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);//可防止hibernate模式下的关联关系子对象中包含父对象造成死循环
JSONArray jsonArray = JSONArray.fromObject(obj,jsonConfig);
return jsonArray;
} /**
* json串或jsonobject对象转成Map
* @param obj
* @return
*/
public static Map<?, ?> jsonToMap(Object obj){
JSONObject jsonObject = null;
if(obj instanceof JSONObject){
jsonObject = (JSONObject)obj;
}else{
jsonObject = JSONObject.fromObject(obj);
} Map<Object, Object> map = new HashMap<Object, Object>(jsonObject.size());
Iterator<?> it = jsonObject.keys();
while(it.hasNext()){
Object key = it.next();
Object value = jsonObject.get(key);
if(value instanceof JSONObject){
map.put(key, jsonToMap(value));
}else if(value instanceof JSONArray){
map.put(key, jsonArrToList(value));
}else{
map.put(key, value);
}
} return map;
} /**
* list串或jsonArray对象转成list
* @param obj
* @return
*/
public static List<?> jsonArrToList(Object obj){ JSONArray jsonArray = null;
if(obj instanceof JSONArray){
jsonArray = (JSONArray)obj;
}else{
jsonArray = JSONArray.fromObject(obj);
} List list = new ArrayList(jsonArray.size());
Iterator<?> it = jsonArray.iterator();
while(it.hasNext()){
Object next = it.next();
if(next instanceof JSONObject){
list.add(jsonToMap(next));
}else if(next instanceof JSONArray){
list.add(jsonArrToList(next));
}else{
list.add(next);
}
} return list;
} /**
* json串转java对象
* @param jsonStr json串
* @param rootClass 要转成的主对象
* @param subClassMap 主对象中包含的list类型的属性Map 格式:subClassMap.put("iordersegments", IorderSegment.class); key是子对象在主对象中的属性名, value是子对象类型
* @return
*/
public static <T>T jsonToBean(String jsonStr,Class<T> rootClass,Map<String, Class> subClassMap){
JsonConfig filterNullConfig = new JsonConfig();
//过滤掉参数值为null的参数,防止后边的时间转换出错
filterNullConfig.setJsonPropertyFilter(new PropertyFilter() {
@Override
public boolean apply(Object clazz, String name, Object value) {
boolean isFilter = false;
if(value==null||"".equals(value)){
isFilter = true;
}
return isFilter;
}
}); JSONObject jsonObject = JSONObject.fromObject(jsonStr,filterNullConfig); String[] dateFormats = new String[] {"yyyy-MM-dd HH:mm:ss","yyyy-MM-dd","yyyy-MM-dd HH:mm"};//不过好像只有 yyyy-MM-dd HH:mm:ss 格式有效
JSONUtils.getMorpherRegistry().registerMorpher(new DateMorpher(dateFormats)); JsonConfig jsonConfig = new JsonConfig();
jsonConfig.setRootClass(rootClass);
if(subClassMap!=null&&subClassMap.size()>0){
jsonConfig.setClassMap(subClassMap);
} return (T)JSONSerializer.toJava(jsonObject, jsonConfig);
} }
自己封装的json工具类的更多相关文章
- 用jackson封装的JSON工具类
package hjp.smart4j.framework.util; import com.fasterxml.jackson.databind.ObjectMapper; import org.s ...
- Spring统一返回Json工具类,带分页信息
前言: 项目做前后端分离时,我们会经常提供Json数据给前端,如果有一个统一的Json格式返回工具类,那么将大大提高开发效率和减低沟通成本. 此Json响应工具类,支持带分页信息,支持泛型,支持Htt ...
- HttpClientUntils工具类的使用测试及注意事项(包括我改进的工具类和Controller端的注意事项【附 Json 工具类】)
HttpClient工具类(我改过): package com.taotao.httpclient; import java.io.IOException; import java.net.URI; ...
- 免费IP代理池定时维护,封装通用爬虫工具类每次随机更新IP代理池跟UserAgent池,并制作简易流量爬虫
前言 我们之前的爬虫都是模拟成浏览器后直接爬取,并没有动态设置IP代理以及UserAgent标识,本文记录免费IP代理池定时维护,封装通用爬虫工具类每次随机更新IP代理池跟UserAgent池,并制作 ...
- springboot封装JsonUtil,CookieUtil工具类
springboot封装JsonUtil,CookieUtil工具类 yls 2019-9-23 JsonUtil public class JsonUtil { private static Obj ...
- 从接口自动化测试框架设计到开发(二)操作json文件、重构json工具类
用例模板里的请求数据多,看起来很乱,所以可以通过访问另外一个文件的方式获取请求数据 把请求数据都放在一个json文件中 取出login的内容: import json fp = open('G:/un ...
- Code片段 : .properties属性文件操作工具类 & JSON工具类
摘要: 原创出处:www.bysocket.com 泥瓦匠BYSocket 希望转载,保留摘要,谢谢! “贵专” — 泥瓦匠 一.java.util.Properties API & 案例 j ...
- Json工具类,实现了反射将整个Object转换为Json对象的功能,支持Hibernate的延迟加
package com.aherp.framework.util; import java.lang.reflect.Array;import java.lang.reflect.Method;imp ...
- Json工具类 - JsonUtils.java
Json工具类,提供Json与对象之间的转换. 源码如下:(点击下载 - JsonUtils.java . gson-2.2.4.jar ) import java.lang.reflect.Type ...
随机推荐
- spring security 管理会话 多个用户不可以使用同一个账号登录系统
多个用户不能使用同一个账号同时登陆系统. 1. 添加监听器 在web.xml中添加一个监听器,这个监听器会在session创建和销毁的时候通知Spring Security. <listener ...
- 理解insert all/insert first的使用
在常用的SQL写法中我们会经常遇到把一个表的数据插入另外一张表的情况,这是一个insert into 表名 select .... from 表名 就可以解决了.但是如果是把一张表的数据同时插入两 ...
- CentOs5.8下安装Oracle12C
12C安装向导: http://docs.oracle.com/database/121/LTDQI/toc.htm 12C下载地址: http://www.oracle.com/technetwor ...
- 杭电ACM 1201
#include <stdio.h> int func(int year){ if ( year % 400 == 0 || (year % 4 == 0 &&year % ...
- 【iCore3 双核心板】例程十一:DMA实验——存储器到存储器的传输
实验指导书及代码包下载: http://pan.baidu.com/s/1bcY5JK iCore3 购买链接: https://item.taobao.com/item.htm?id=5242294 ...
- MVC下载文件方式
MVC下载文件方式 http://www.cnblogs.com/liang--liang/archive/2012/10/20/2732745.html 方式一: public FileStream ...
- openfire3.9.1 开发配置
1.在官网上下载最新的openfire源码 eg:openfire_src_3.9.1.zip 大概是一百多M 2.解压源码文件 一下步骤参考此同学的博客:http://redhacker.ite ...
- 当div自适应的高度超过预设的高度的时候出现滚动条的办法
方法一:主要是 min-height:50px; max-height:200px;overflow: auto; <div id="ss" style="widt ...
- jquery设置checkbox状态,设置dropdownlist选中值,隐藏某控件,给某控件追加东西
jquery设置checkbox状态 $("[ID$=chkType]").attr("checked", true); jquery设置dropdownlis ...
- Asp.net mvc web api 在项目中的实际应用
Asp.net mvc web api 在项目中的实际应用 前言:以下只是记录本人在项目中的应用,而web api在数据传输方面有多种实现方式,具体可根据实际情况而定! 1:数据传输前的加密,以下用到 ...