json格式字符串与java.util.Map的互转(借助于jackson.jar)

 

package com.test.javaAPI.json;

/**
 * json工具类
 * 
 * @author Wei
 * @time 2016年10月2日 下午4:25:25
 */
public class UtilJackson {
	public static String jsonStr = "{\"parameters\":{\"PI_CAE574\":\"20160908\",\"PI_JSRQ\":\"20160908\",\"Pi_CAE920\":\"301\",\"Pi_YAE601\":\"1\",\"PI_BAE001\":\"511502\",\"Pi_JFDX\":\"1\",\"\":\"pkg_weiyl.getMatchResult\"},\"serviceId\":\"directJdbcService\",\"method\":\"savePointProcedure\"}";
	/**
	 * 比较复杂的json格式字符串
	 */
	public static String jsonStr_HN = "{\"DataPackage\":{\"JZZL\":[{\"CY\\\"BZ\":\"2\",\"CYQK\":\"\",\"CYRQ\":\"20090205\",\"CYZD\":\"霍乱\",\"CYZD1\":\"\",\"CYZDGJDM\":\"A00.901\",\"JZJLH\":\"136\",\"JZLB\":\"21\",\"RYRQ\":\"20090205\",\"RYZD\":\"霍乱\",\"RYZD1\":\"\",\"RYZDGJDM\":\"A00.901\",\"RYZS\":\"\",\"SSMC\":\"\",\"YYBH\":\"1000\",\"ZYH\":\"\",\"ZYJSLB\":\"1\"},{\"CYBZ\":\"2\",\"CYQK\":\"\",\"CYRQ\":\"20090202\",\"CYZD\":\"霍乱\",\"CYZD1\":\"\",\"CYZDGJDM\":\"A00.901\",\"JZJLH\":\"142\",\"JZLB\":\"21\",\"RYRQ\":\"20090201\",\"RYZD\":\"霍乱\",\"RYZD1\":\"\",\"RYZDGJDM\":\"A00.901\",\"RYZS\":\"\",\"SSMC\":\"\",\"YYBH\":\"1000\",\"ZYH\":\"\",\"ZYJSLB\":\"1\"},{\"CYBZ\":\"2\",\"CYQK\":\"\",\"CYRQ\":\"20090304\",\"CYZD\":\"霍乱\",\"CYZD1\":\"\",\"CYZDGJDM\":\"A00.901\",\"JZJLH\":\"132\",\"JZLB\":\"21\",\"RYRQ\":\"20090303\",\"RYZD\":\"霍乱\",\"RYZD1\":\"\",\"RYZDGJDM\":\"A00.901\",\"RYZS\":\"\",\"SSMC\":\"\",\"YYBH\":\"1000\",\"ZYH\":\"\",\"ZYJSLB\":\"1\"},{\"CYBZ\":\"2\",\"CYQK\":\"\",\"CYRQ\":\"20090306\",\"CYZD\":\"nihao\",\"CYZD1\":\"\",\"CYZDGJDM\":\"\",\"JZJLH\":\"140\",\"JZLB\":\"21\",\"RYRQ\":\"20090305\",\"RYZD\":\"nihao\",\"RYZD1\":\"\",\"RYZDGJDM\":\"\",\"RYZS\":\"\",\"SSMC\":\"\",\"YYBH\":\"1000\",\"ZYH\":\"\",\"ZYJSLB\":\"1\"}]},\"FunctionParams\":{\"FHZ\":\"1\",\"GET_PAGE_TOTAL\":\"4\",\"HAS_NEXT_REQUEST\":\"false\",\"MSG\":\"执行成功!\",\"SESSIONID\":\"QKSZPNrRPptTGs3ymqvJhQLZyxKJpd4XCHGJQBGFQcFQtwRYGvxS!306292812!1338878737991\"}}";
	public static String jsonStr_KEY_DataPackage = "DataPackage";
	public static String jsonStr_KEY_FunctionParams = "FunctionParams";
	public static String jsonStr4 = "{\"verified\":false,\"name\":{\"last\":\"Hankcs\",\"first\":\"Joe\"},\"userImage\":\"Rm9vYmFyIQ==\",\"gender\":\"MALE\"}";
	public static String jsonStr4_KEY1 = "verified";
}

  

 

package com.test.javaAPI.json;

import java.io.IOException;
import java.util.Map;

import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.junit.Test;

import com.sun.xml.internal.messaging.saaj.util.ByteOutputStream;

public class JacksonTest {
	public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException {
		// ObjectMapper objMap = new ObjectMapper();
		// // Map map = objMap.readValue(UtilJackson.jsonStr, Map.class);
		// Map map = objMap.readValue(UtilJackson.jsonStr_HN, Map.class);
		// System.out.println(map.toString());
		// Set set = map.keySet();
		// Iterator it = set.iterator();
		// while (it.hasNext()) {
		// Object key = (Object) it.next();
		// Object value = map.get(key);
		// System.out.println("key:" + key + ",value:" + value);
		// }

		// new JacksonTest().removeDataPackage("DataPackage");
		String str = new JacksonTest().removeJsonObjByKey(UtilJackson.jsonStr4,
				UtilJackson.jsonStr4_KEY1);
		System.out.println(str);
	}

	/**
	 * 方法的作用:去除一个json格式字符串的某一个key 删除 这个json字符串里的这个key对应的对象 该方法与框架中的 String
	 * cn.sinobest.framework.web.his.JsonManager.removeDataPackage(String
	 * jsonStr) 这个方法的功能一致
	 * 
	 * @param jsonKey
	 * @return
	 * @throws JsonParseException
	 * @throws JsonMappingException
	 * @throws IOException
	 */
	@Test
	public String removeDataPackage(String jsonKey) throws JsonParseException, JsonMappingException, IOException {
		ObjectMapper objMap = new ObjectMapper();
		Map map = objMap.readValue(UtilJackson.jsonStr_HN, Map.class);
		// map.remove("DataPackage");
		map.remove(jsonKey);
		ByteOutputStream bops = new ByteOutputStream();
		objMap.writeValue(bops, map);
		System.out.println(bops.toString());
		return null;
	}

	/**
	 * 
	 * @param jsonStr
	 * @param key
	 * @return
	 * @throws IOException
	 * @throws JsonMappingException
	 * @throws JsonParseException
	 */
	public String removeJsonObjByKey(String jsonStr, String key)
			throws JsonParseException, JsonMappingException, IOException {
		ObjectMapper objMap = new ObjectMapper();
		// 1 把json格式字符串转换为 java.util.Map
		Map map = objMap.readValue(jsonStr, Map.class);
		// 2 删除map中的对应key的项目
		map.remove(key);
		// 准备字节流,接收ObjectMapper中写出的输出流
		ByteOutputStream bops = new ByteOutputStream();
		// 3 把map重新转换为json格式字符串
		objMap.writeValue(bops, map);
		if (!"".equals(bops)) {
			return bops.toString();
		}
		return "";
	}

}

  

控制台输出:

{"name":{"last":"Hankcs","first":"Joe"},"userImage":"Rm9vYmFyIQ==","gender":"MALE"}

http://tool.oschina.net/codeformat/json 这个json字符串格式化工具格式化后的效果:

 

 

{
    "name": {
        "last": "Hankcs", 
        "first": "Joe"
    }, 
    "userImage": "Rm9vYmFyIQ==", 
    "gender": "MALE"
}


posted @ 2016-10-02 19:11  Sunor  阅读(5661)  评论(0编辑  收藏  举报