java jsonString与Map之间转换

public static void main(String[] args) {
	Map<String, String> map = new HashMap<String, String>();
	map.put("key1", "value1");
	map.put("key2", "value2");
	map.put("key3", "value3");
	System.out.println("source: " + map.toString());
	
	// map转换成jsonString
	String jsonStr = JSON.toJSONString(map);
	System.out.println("jsonStr: " + jsonStr);
	
	// jsonString转换成Map
	Map<String, String> jsonMap = JSON.parseObject(jsonStr, new TypeReference<HashMap<String, String>>() {
	});
	System.out.println("jsonMap: " + jsonMap.toString());
}

执行结果:

source: {key1=value1, key2=value2, key3=value3}
jsonStr: {"key1":"value1","key2":"value2","key3":"value3"}
jsonMap: {key1=value1, key2=value2, key3=value3}

 

posted @ 2023-01-19 15:05  锐洋智能  阅读(1625)  评论(0编辑  收藏  举报