Json和Map互转,四个包(org.json/net.sf.json/com.google.gson/com.alibaba.fastjson)

目前使用的(org.json/net.sf.json/com.google.gson/com.alibaba.fastjson)这四种json-map互转,其他的以后在补充。。。。。。。。。。。。。。

导入的jar有:

commons-beanutils-1.6.1.jar

commons-lang-2.1.jar

ezmorph-1.0.3.jar

jackson-all-1.8.5.jar

gson-2.2.4.jar

json-lib-2.2.2-jdk15.jar

json.jar

fastjson-1.1.32.jar

/**
 * 
 */
package map2json;

import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;

import net.sf.json.JSONArray;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;

/**
 * @author hy
 * @date 2019-02-25 15:45:35
 * 
 */
public class map2json {

	public static void main(String[] args) {
		map2jsonstr1();
		map2jsonstr2();
		map2jsonstr3();
		map2jsonstr4();
	}

	// net.sf.json包
	public static void map2jsonstr1() {
		Map<String, Object> map = new LinkedHashMap<String, Object>();
		map.put("1", "a");
		map.put("2", "b");
		map.put("3", "c");
		map.put("4", "d");
		map.put("5", "e");
		map.put("6", new String[] { "aa", "bb" });
		// 多个不同包的同名类,需要指明指哪个包里的
		net.sf.json.JSONObject jo = net.sf.json.JSONObject.fromObject(map);
		System.out.println(jo.toString());
		// 数组
		JSONArray json = JSONArray.fromObject(map);
		System.out.println(json.toString());
		// 将json数据再转回map
		net.sf.json.JSONObject myJson = net.sf.json.JSONObject.fromObject(map);
		@SuppressWarnings("unchecked")
		Map<Object, Object> m = myJson;
		for (Entry<Object, Object> entry : m.entrySet()) {
			System.out.println(entry.getKey().toString() + ":"
					+ entry.getValue().toString());
		}
	}

	// org.json包
	public static void map2jsonstr2() {
		Map<String, String> map = new LinkedHashMap<String, String>();
		map.put("1", "a");
		map.put("2", "b");
		map.put("3", "c");
		map.put("4", "d");
		map.put("5", "e");
		// org.json
		org.json.JSONObject js = new org.json.JSONObject(map);
		System.out.println(js.toString());
		Map<Object, Object> ma = new HashMap<>();
		@SuppressWarnings("rawtypes")
		Iterator it = js.keys();
		while (it.hasNext()) {
			String key = (String) it.next();
			// 得到value的值
			Object value = js.get(key);
			// System.out.println(key+":"+valStr);
			ma.put(key, value.toString());

		}
		for (Entry<Object, Object> mp : ma.entrySet()) {
			System.out.println(mp.getKey() + ":" + mp.getValue());
		}
	}

	// com.google.gson包
	public static void map2jsonstr3() {

		Map<String, String> map = new LinkedHashMap<String, String>();
		map.put("1", "a");
		map.put("2", "b");
		map.put("3", "c");
		map.put("4", "d");
		map.put("5", "e");

		Gson gson = new Gson();
		String jsonStr = gson.toJson(map);
		System.out.println(jsonStr);

		Map<Object, Object> ma = new HashMap<>();
		ma = gson.fromJson(jsonStr, Map.class);

		for (Entry<Object, Object> mp : ma.entrySet()) {
			System.out.println(mp.getKey() + ":" + mp.getValue());
		}

	}

	// com.alibaba.fastjson包
	public static void map2jsonstr4() {

		Map<String, String> map = new LinkedHashMap<String, String>();
		map.put("1", "a");
		map.put("2", "b");
		map.put("3", "c");
		map.put("4", "d");
		map.put("5", "e");
		// map转json
		com.alibaba.fastjson.JSONObject jsonObject = (JSONObject) com.alibaba.fastjson.JSONObject
				.toJSON(map);
		String alijson = jsonObject.toJSONString();
		System.out.println(alijson);
		// json转map
		/*
		 * Map<String, String> maps = (Map<String, String>) JSON.parse(alijson);
		 * for (Entry<String, String> alima :maps.entrySet()) {
		 * System.out.println(alima.getKey()+":"+alima.getValue()); }
		 */
		Map alimap = JSON.parseObject(alijson, Map.class);
		for (Object obj : alimap.keySet()) {
			System.out.println(obj + ":" + alimap.get(obj));
		}
	}

}

  

原文地址:https://www.cnblogs.com/hyblogs/p/10432471.html