FastJson 工具类
2017-03-14 13:16
176 查看
package com.cmh.util;
import java.util.List;
import java.util.Map;
import javax.xml.crypto.dsig.keyinfo.KeyValue;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.JSONLibDataFormatSerializer;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SerializerFeature;
/**
* fastjson工具类
* @version:1.0.0
*/
public class FastJsonUtils {
}
需要导入包
fastjson
import java.util.List;
import java.util.Map;
import javax.xml.crypto.dsig.keyinfo.KeyValue;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.JSONLibDataFormatSerializer;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SerializerFeature;
/**
* fastjson工具类
* @version:1.0.0
*/
public class FastJsonUtils {
private static final SerializeConfig config; static { config = new SerializeConfig(); config.put(java.util.Date.class, new JSONLibDataFormatSerializer()); // 使用和json-lib兼容的日期输出格式 config.put(java.sql.Date.class, new JSONLibDataFormatSerializer()); // 使用和json-lib兼容的日期输出格式 } private static final SerializerFeature[] features = { SerializerFeature.WriteMapNullValue, // 输出空置字段 SerializerFeature.WriteNullListAsEmpty, // list字段如果为null,输出为[],而不是null SerializerFeature.WriteNullNumberAsZero, // 数值字段如果为null,输出为0,而不是null SerializerFeature.WriteNullBooleanAsFalse, // Boolean字段如果为null,输出为false,而不是null SerializerFeature.WriteNullStringAsEmpty // 字符类型字段如果为null,输出为"",而不是null }; public static String toJSONString(Object object) { return JSON.toJSONString(object, config, features); } public static String toJSONNoFeatures(Object object) { return JSON.toJSONString(object, config); } public static Object toBean(String text) { return JSON.parse(text); } public static <T> T toBean(String text, Class<T> clazz) { return JSON.parseObject(text, clazz); } // 转换为数组 public static <T> Object[] toArray(String text) { return toArray(text, null); } // 转换为数组 public static <T> Object[] toArray(String text, Class<T> clazz) { return JSON.parseArray(text, clazz).toArray(); } // 转换为List public static <T> List<T> toList(String text, Class<T> clazz) { return JSON.parseArray(text, clazz); } /** * 将javabean转化为序列化的json字符串 * @param keyvalue * @return */ public static Object beanToJson(KeyValue keyvalue) { String textJson = JSON.toJSONString(keyvalue); Object objectJson = JSON.parse(textJson); return objectJson; } /** * 将string转化为序列化的json字符串 * @param keyvalue * @return */ public static Object textToJson(String text) { Object objectJson = JSON.parse(text); return objectJson; } /** * json字符串转化为map * @param s * @return */ public static Map stringToCollect(String s) { Map m = JSONObject.parseObject(s); return m; } /** * 将map转化为string * @param m * @return */ public static String collectToString(Map m) { String s = JSONObject.toJSONString(m); return s; }
}
需要导入包
fastjson
相关文章推荐
- 【职坐标】使用FastJson创建Java Json解析工具类
- 用fastjson框架解析Json数据工具类
- Android应用开发------------Json工具类(fastJson和Gson的两种工具类)
- FastJSON工具类测试
- JsonUtils ~~fastJson解析工具类
- java中将javabean转为json的两种第三方工具类Fastjson&&Jackson
- fastjson解析json工具类
- java json工具类(FastJson)
- 【FastJSON】将FastJSON作为工具类集成到项目中
- 利用jackson/fastjson封装JSON转换工具类
- Json转换工具类(基于google的Gson和阿里的fastjson)
- fastjson 与jackson 工具类封装
- FastJSON工具类测试
- JSON字符串转换为Map (fastjson)
- 闲来无事写JSONのFastJson序列化
- 解析json工具类
- JSON工具学习记录--FastJSON循环引用问题
- fastjson和json-lib的区别
- JSON 之FastJson解析
- 将map类型用fastjson转换成json