Java FastJson 的使用

项目地址: https://github.com/alibaba/fastjson

引入jar包

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.59</version>
</dependency>

FastJson 中 Java对象转成JSON字符串

public class FastJsonUtils
{

  public static String jsonEncode(Object obj)
  {
    return JSON.toJSONString(obj,
        SerializerFeature.DisableCircularReferenceDetect, // 禁止循环引用
        SerializerFeature.WriteNullStringAsEmpty, // null string  to ''
        SerializerFeature.WriteNullListAsEmpty, // NullList > empty
        SerializerFeature.WriteNullBooleanAsFalse, // NullBoolean as false
        SerializerFeature.WriteMapNullValue);
  }
}

注意:

转成字符串时, 一定要禁用循环引用,比如你把Map对象转成json时,如果有相同的对象put进入的时,相同对象转成字符为ref

FastJson 中 JSON字符串转成Java对象

public class FastJsonUtils
{
  public static <T> T jsonDecode(String json, Class<T> clazz)
	{
		try {
			return (T)JSON.parseObject(json, clazz);
		} catch (Exception e) {
			//LOG.debug("parse object error and the json is = " + json , e);
		}
		return null;
	}
}

该方法会抛异常,所以为了开发上便利,这里直接给它 try-catch

FastJson 中 JSON字符串转成集合数组

public class FastJsonUtils
{
  public static <T> List<T> parseArray(String json, Class<T> clazz)
	{
		try {
			return JSONObject.parseArray(json, clazz);
		} catch (Exception e) {
		}
		return null;
	}
}

Java FastJson 示例

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.pangugle.framework.log.Log;
import com.pangugle.framework.log.LogFactory;

public class FastJsonUtils {

	private static Log LOG = LogFactory.getLog(FastJsonUtils.class);

	public static String jsonEncode(Object obj)
	{
		return JSON.toJSONString(obj,
				SerializerFeature.DisableCircularReferenceDetect, // 禁止循环引用
				SerializerFeature.WriteNullStringAsEmpty,// null string  to ''
				SerializerFeature.WriteNullListAsEmpty,// NullList > empty
				SerializerFeature.WriteNullBooleanAsFalse,// NullBoolean as false
				SerializerFeature.WriteMapNullValue);
	}

	public static <T> T jsonDecode(String json, Class<T> clazz)
	{
		try {
			return (T)JSON.parseObject(json, clazz);
		} catch (Exception e) {
			//LOG.debug("parse object error and the json is = " + json , e);
		}
		return null;
	}

	public static JSONObject toJSONObject(String jsonString) {
		JSONObject json = null;
		try {
			json = JSON.parseObject(jsonString);
		} catch (Exception e) {
			//LOG.error("to json objec error, and the json " + jsonString, e);
		}
		return json;
	}

	public static <T> List<T> parseArray(String json, Class<T> clazz)
	{
		try {
			return JSONObject.parseArray(json, clazz);
		} catch (Exception e) {
		}
		return null;
	}

	public static JSONArray parseArray(String json)
	{
		try {
			return JSONObject.parseArray(json);
		} catch (Exception e) {
			//LOG.error("parse array error, and then json = " + json, e);
		}
		return null;
	}


	@SuppressWarnings("unchecked")
	public static Map<String, Object> toMap(String jsonString) {
		Map<String, Object> map = JSON.parseObject(jsonString, Map.class);
		return map;
	}

	/**
	 * 获取第一个key值
	 *
	 * @return
	 */
	public static String getFirstKey(JSONObject jsonObject) {
		String obj = null;
		for (String str : jsonObject.keySet()) {
			obj = str;
			if (obj != null) {
				break;
			}
		}
		return  obj;
	}

	public static void main(String[] args) {
		Map<String, Object> json = new HashMap<String, Object>();
		json.put("test", 1);

		String jsonString = jsonEncode(json);
		System.out.println("对象转成JSON字符串 : " + jsonString);
		System.out.println("JSON字符串转成对象 : ");
		JSONObject jsonObj = jsonDecode(jsonString, JSONObject.class);
		System.out.println(jsonObj);
	}

}