【FastJSON】将FastJSON作为工具类集成到项目中
2014-09-28 12:59
351 查看
FastJSON是Alibaba产出的json处理框架 (JSON parser + JSON generator)。仅依赖于JRE。
以下是工具代码:
import java.util.Date;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.serializer.SimpleDateFormatSerializer;
/**
* @ClassName FastJsonUtils
* @Description 简单封装alibaba产出的json处理框架 (JSON parser + JSON generator)
* @Date 2014年9月28日
*/
public class FastJsonUtils{
private static SerializeConfig mapping = new SerializeConfig();
private static String dateFormat;
static {
dateFormat = "yyyy-MM-dd HH:mm:ss";
mapping.put(Date.class, new SimpleDateFormatSerializer(dateFormat));
}
/**
* @Title: obj2Json
* @Description: 将对象转换为JSON字符串
* @param obj //对象模型
* @return String //转换完毕的字符串
*/
public static String obj2Json(Object obj) {
String str = JSON.toJSONString(obj,mapping,SerializerFeature.WriteMapNullValue);
return str;
}
/**
* @Title: json2Obj
* @Description: 将JSON字符串转换为对象
* @param text //需要转换的字符串
* @param clazz //转换时使用的实体类型
* @return
*/
public static <X> X json2Obj(String text, Class<X> clazz) {
X x =JSON.parseObject(text, clazz);
return x;
}
}
以下是工具代码:
import java.util.Date;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.serializer.SimpleDateFormatSerializer;
/**
* @ClassName FastJsonUtils
* @Description 简单封装alibaba产出的json处理框架 (JSON parser + JSON generator)
* @Date 2014年9月28日
*/
public class FastJsonUtils{
private static SerializeConfig mapping = new SerializeConfig();
private static String dateFormat;
static {
dateFormat = "yyyy-MM-dd HH:mm:ss";
mapping.put(Date.class, new SimpleDateFormatSerializer(dateFormat));
}
/**
* @Title: obj2Json
* @Description: 将对象转换为JSON字符串
* @param obj //对象模型
* @return String //转换完毕的字符串
*/
public static String obj2Json(Object obj) {
String str = JSON.toJSONString(obj,mapping,SerializerFeature.WriteMapNullValue);
return str;
}
/**
* @Title: json2Obj
* @Description: 将JSON字符串转换为对象
* @param text //需要转换的字符串
* @param clazz //转换时使用的实体类型
* @return
*/
public static <X> X json2Obj(String text, Class<X> clazz) {
X x =JSON.parseObject(text, clazz);
return x;
}
}
相关文章推荐
- springmvc项目使用fastjson作为json处理器时出现重复引用、循环引用的解决办法
- J2EE 工程中使用JSON作为协议的话,可以使用Fastjson作为更好的替代了
- 项目ITP(四) javaweb http json 交互 in action (服务端 spring 手机端 提供各种工具类)勿喷!
- springMvc与fastJson 集成时日期格式的问题
- 关于ringpringvc集成fastjson后空值默认去掉空值的处理后修改显示空值
- FastJson 工具类
- 由于Python本身自带的界面库功能并不强大,我们使用Python+wxPython作为界面开发平台,在公司新的项目中进行界面开发。开发过程使用Eclipse+PyDev 作为集成开发环境。产品发布时使用Py2exe进行打包。如今,在我的团队中,Pytho
- Spring MVC4设置使用fastjson作为json解析器,替代jackson
- Retrofit2 使用FastJson作为Converter
- fastjson解析json工具类
- fastjson 与jackson 工具类封装
- 项目ITP(四) javaweb http json 交互 in action (服务端 spring 手机端 提供各种工具类)勿喷!
- Android应用开发------------Json工具类(fastJson和Gson的两种工具类)
- 作为一个屌丝程序员不得不收藏的工具类 一 json工具类
- Spring MVC4设置使用fastjson作为json解析器,替代jackson
- FastJSON工具类测试
- java web项目中集成Jetty作为web容器
- 用fastjson框架解析Json数据工具类
- JsonUtils ~~fastJson解析工具类
- SpringMVC+FastJson+Swagger集成完整示例