自己封装的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 ...
随机推荐
- 对Oracle10g rac ons服务的一些理解
1.什么是ONS ONS(Oracle Notification Service)是Oracle Clusterware 实现FAN Event Push模型的基础. 在传统模型中,客户端需要 ...
- SPFA导读及介绍(转载)
适用范围:给定的图存在负权边,这时类似Dijkstra等算法便没有了用武之地,而Bellman-Ford算法的复杂度又过高,SPFA算法便派上用场了. 我们约定有向加权图G不存在负权回路,即最短路径一 ...
- Unity UGUI 裁剪TTF字体
BitBucket上找到了一个perl工程,font-optimizer.拉取代码到本地.为了运行它,还需要装Perl解释器,可以在Perl的官网上下载ActivePerl.装好ActivePerl后 ...
- scrum站立会议学习
项目:连连看游戏 小组名称:临时小组 组长:张政 小组成员: 李权 武志远 张政 张金生 MASTER:张政 会议内容: 一.已完成项: 1.根据先前的安排和计划完成了项目主要环境的搭建,配置好了基本 ...
- IOS第七天(6:UiTableView编辑模式, 拖动位置 ,滑动删除)
**********UiTableView编辑模式, 拖动位置 ,滑动删除 #import "HMViewController.h" @interface HMViewContro ...
- dom4j解析xml文档(增删改查)
package itcast.dom4j; import java.io.File; import java.io.FileOutputStream; import java.io.FileWrite ...
- CentOS下搭建nginx+php环境
一.下载安装nginx 参见 http://www.cnblogs.com/kreo/p/4378086.html 不再赘述 二.下载php #下载 wget http://bg2.php.net/d ...
- 使用 HTML5 input 类型提升移动端输入体验
在过去的几年里,在移动设备上浏览网页已变得难以置信的受欢迎. 但是这些设备上的浏览体验,有时遗留很多的有待改进.当涉及到填写表单时,这一点尤为明显.幸运的是,HTML5规范引入了许多新input类型, ...
- entity.Student@150f3932, entity.Student@1a740c6b 没有实体中的数据
public class AppServerAction extends BaseAction { /** * */ /** * 初始化 “我的产品”列表 JSP页面 ...
- 怎样将BigDecimal转换成Int
BigDecimal a=new BigDecimal(12.88); int b=a.intValue(); System.out.println(b);//b=12;