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

目前使用的(org.json/net.sf.json/com.google.gson)这三种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

/**
 * 
 */
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.google.gson.Gson;


/**
 * @author hy
 * @date 2019-02-25 15:45:35
 *
 */
public class map2json {
        
    public static void main(String[] args) {
        //map2jsonstr1();
        //map2jsonstr2();
        map2jsonstr3();
    }
    
//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());
    }
}

    
    
@SuppressWarnings("unchecked")
//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());
        }
     
    }
    
}

猜你喜欢

转载自www.cnblogs.com/hyblogs/p/10432471.html