`

Map与Json互相转换

    博客分类:
  • JAVA
 
阅读更多
/*******************************************************************************
 * @project: alms
 * @package: com.acconsys.alms.util
 * @file: MapJsonUtil.java
 * @author: Malt
 * @created: 2013-9-27
 * @purpose:
 * 
 * @version: 1.0
 * 
 * Revision History at the end of file.
 * 
 * Copyright 2013 AcconSys All rights reserved.
 ******************************************************************************/

package com.acconsys.alms.util;

import java.util.Map;
import net.sf.json.JSONObject;

public class MapJsonUtil {
	
	/**
	 * String(json) conversion Map.
	 * @param strJSON
	 * @return
	 * @throws JSONException
	 */
	@SuppressWarnings("unchecked")
	public static Map<String,Object> StrJSON2Map(String strJSON){
		JSONObject jsonObj = JSONObject.fromObject(strJSON); 
		return (Map<String, Object>) JSONObject.toBean(jsonObj,Map.class);
	}
	
	/**
	 * Map conversion String(json).
	 * @param map
	 * @return
	 */
	public static String MapToStrJSON(Map<String,Object> map) {
		String jsonStr = null;
		jsonStr = JSONObject.fromObject(map).toString();
		return jsonStr;
	}
	
	public static void main(String[] args) {
		System.out.println(StrJSON2Map("{\"A\":\"AV\"}").get("A"));
		System.out.println();
		System.out.println(MapToStrJSON(StrJSON2Map("{\"A\":\"AV\"}")));
	}
}


/*******************************************************************************
 * <B>Revision History</B><BR>
 * [type 'revision' and press Alt + / to insert revision block]<BR>
 * 
 * 
 * 
 * Copyright 2013 AcconSys All rights reserved.
 ******************************************************************************/


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics