将java.util.Map转换为格式化json

import java.text.SimpleDateFormat;
import java.util.Map;
import java.util.Set;

public class ConvertMap2Json
{
	private static final String QUOTE = "\"";

	/**
	 * 将Map转成一个json对象,即像大括号的东西
	 * e.g.
	 * {
	 * 		"eventName": "name"
	 * }
	 *
	 * @param body
	 * @param tabCount
	 * @param addComma
	 * @return
	 */
	public static String buildJsonBody(Map body, int tabCount, boolean addComma)
	{
		StringBuilder sbJsonBody = new StringBuilder();
		sbJsonBody.append("{\n");
		Set keySet = body.keySet();
		int count = 0;
		int size = keySet.size();
		for (String key : keySet)
		{
			count++;
			sbJsonBody.append(buildJsonField(key, body.get(key), tabCount + 1, count != size));
		}
		sbJsonBody.append(getTab(tabCount));
		sbJsonBody.append("}");
		return sbJsonBody.toString();
	}

	/**
	 * 生成一个json字段即像这样的
	 * e.g.
	 * "eventName": "aaa"
	 *
	 * @param key
	 * @param value
	 * @param tabCount
	 * @param addComma
	 * @return
	 */
	private static String buildJsonField(String key, Object value, int tabCount, boolean addComma)
	{
		StringBuilder sbJsonField = new StringBuilder();
		sbJsonField.append(getTab(tabCount));
		sbJsonField.append(QUOTE).append(key).append(QUOTE).append(": ");
		sbJsonField.append(buildJsonValue(value, tabCount, addComma));
		return sbJsonField.toString();
	}

	/**
	 * 生成json值对象
	 * e.g.
	 * "string"
	 * 或
	 * {
	 * 		"key": "value"
	 * }
	 *
	 * @param value
	 * @param tabCount
	 * @param addComma
	 * @return
	 */
	private static String buildJsonValue(Object value, int tabCount, boolean addComma)
	{
		StringBuilder sbJsonValue = new StringBuilder();
		if (value instanceof String)
		{
			sbJsonValue.append(QUOTE).append(value).append(QUOTE);
		}
		else if (value instanceof Integer || value instanceof Long || value instanceof Double)
		{
			sbJsonValue.append(value);
		}
		else if (value instanceof java.util.Date)
		{
			sbJsonValue.append(QUOTE).append(formatDate((java.util.Date) value)).append(QUOTE);
		}
		else if (value.getClass().isArray() || value instanceof java.util.Collection)
		{
			sbJsonValue.append(buildJsonArray(value, tabCount, addComma));
		}
		else if (value instanceof java.util.Map)
		{
			sbJsonValue.append(buildJsonBody((java.util.Map) value, tabCount, addComma));
		}
		sbJsonValue.append(buildJsonTail(addComma));
		return sbJsonValue.toString();
	}

	/**
	 * 生成json数组对象
	 * [
	 * 		"value"
	 * ]
	 * 或
	 * [
	 * 		{
	 * 			"key": "value"
	 * 		}
	 * ]
	 *
	 * @param value
	 * @param tabCount
	 * @param addComma
	 * @return
	 */
	private static String buildJsonArray(Object value, int tabCount, boolean addComma)
	{
		StringBuilder sbJsonArray = new StringBuilder();
		sbJsonArray.append("[\n");
		Object[] objArray = null;
		if (value.getClass().isArray())
		{
			objArray = (Object[]) value;
		}
		else if (value instanceof java.util.Collection)//将集合类改为对象数组
		{
			objArray = ((java.util.Collection) value).toArray();
		}
		int size = objArray.length;
		int count = 0;
		//循环数组里的每一个对象
		for (Object obj : objArray)
		{
			//加上缩进,比上一行要多一个缩进
			sbJsonArray.append(getTab(tabCount + 1));
			//加上对象值,比如一个字符串"String",或者一个对象
			sbJsonArray.append(buildJsonValue(obj, tabCount + 1, ++count != size));
		}
		sbJsonArray.append(getTab(tabCount));
		sbJsonArray.append("]");
		return sbJsonArray.toString();
	}

	/**
	 * 加上缩进,即几个\t
	 * TODO.
	 *
	 * @param count
	 * @return
	 */
	private static String getTab(int count)
	{
		StringBuilder sbTab = new StringBuilder();
		while (count-- > 0)
		{
			sbTab.append("\t");
		}
		return sbTab.toString();
	}

	/**
	 * 加上逗号
	 * TODO.
	 *
	 * @param addComma
	 * @return
	 */
	private static String buildJsonTail(boolean addComma)
	{
		return addComma ? ",\n" : "\n";
	}

	/**
	 * 格式化日期
	 * TODO.
	 *
	 * @param date
	 * @return
	 */
	private static String formatDate(java.util.Date date)
	{
		return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
	}
}


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

import org.junit.Test;

public class TestConvertMap2Json
{

	@Test
	public void test()
	{
		Map data = initData();
		System.out.println(ConvertMap2Json.buildJsonBody(data, 0, false));
	}

	private static Map initData()
	{
		Map map = new HashMap();
		map.put("eventName", "ApplicationDetailUpdateAfter");
		map.put("source", "11111-22222-33333");
		map.put("age", 123.333);
		
		Map paramMap = new HashMap();
		map.put("parameters", paramMap);
		
		paramMap.put("recordId", "REC14-00000-00013");
		paramMap.put("recordType", "ggg-aaa-cc-dd");
		
		List countryList = new ArrayList();
		map.put("country", countryList);
		countryList.add("China");
		countryList.add("USA");
		
		List> mapList = new ArrayList>();
		map.put("mapList", mapList);
		Map tempMap = new HashMap();
		tempMap.put("name", "zzz");
		tempMap.put("nick", "ttt");
		mapList.add(tempMap);
		return map;
	}
	
}


 
  

{
	"mapList": [
		{
			"nick": "ttt",
			"name": "zzz"
		}
	],
	"source": "11111-22222-33333",
	"age": 123.333,
	"eventName": "ApplicationDetailUpdateAfter",
	"parameters": {
		"recordId": "REC14-00000-00013",
		"recordType": "ggg-aaa-cc-dd"
	},
	"country": [
		"China",
		"USA"
	]
}


你可能感兴趣的:(java基础)