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

桃花島主黃老師發表於2019-02-25

目前使用的(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());
        }
     
    }
    
}

 

相關文章