Different json and map Huzhuan

package com.json;

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

import org.json.JSONArray;

import com.google.gson.Gson;

public class jsonTest2 {

    public static void main(String[] args) {
        Map<Object, Object> map = new LinkedHashMap<Object, Object>();
        for (int i = 1; i <= 180000; i++) {
//            map.put(String.valueOf(i), new String[] { "aa", "bb" ,"cc","dd","ee"});
//            map.put(String.valueOf(i), "aa");
            map.put(String.valueOf(i), new String[] { "aa", "bb" });
        }
        
        
                
//        System.out.println(".........net.sf.json.........");
//        long startTime = System.currentTimeMillis(); // 获取开始时间
//        net.sf.json.JSONObject jo = net.sf.json.JSONObject.fromObject(map);
//        long endTime = System.currentTimeMillis(); // 获取结束时间
//        System.out.println ( "map into net.sf.json.JSONObject running time:" + (endTime - startTime) + "MS");
 //         
//         Long startTime2 = System.currentTimeMillis (); // get start time
 //         the map <Object, Object> Jo = m;
 //         Long endTime2 = System.currentTimeMillis (); // Get end time
 //         System.out.println ( "Switch map net.sf.json.JSONObject runtime : "+ (endTime2 - startTime2) +" MS ");
 //         
//         System.out.println (" org.json .......... ........ ");
 / /         Long startTimeOrg = System.currentTimeMillis (); // Get end time
 //         org.json.JSONObject new new JS = ORG.
json.JSONObject(map);
//         Long endTimeOrg = System.currentTimeMillis (); // Gets the end time
 //         System.out.println ( "org.json.JSONObject map into running time:" + (endTimeOrg - startTimeOrg) + "MS");
 / /         
//         Long startTimeOrg2 = System.currentTimeMillis (); // acquisition start time
 //         the Map <Object, Object> = new new mA the HashMap ();
 //         @SuppressWarnings ( "rawtypes")
 //         the Iterator js.keys IT = ( );
 //         the while (it.hasNext ()) {
 //             String Key = (String) it.next ();
 //             Object value = js.get (Key);
 //             ma.put (Key, value.toString ());
//         }
 //         Long endTimeOrg2 = System.currentTimeMillis (); // Get end time
 //         System.out.println ( "Run Time Map Switch org.json.JSONObject:" + (endTimeOrg2 - startTimeOrg2) + "ms" );        
 //         
//         System.out.println ( "Gson .......... ........");
 //         Long startTimeGson = System.currentTimeMillis ();
 //         Gson GSON new new Gson = ();
 //         String jsonStr = gson.toJson (the Map);
 //         Long endTimeGson = System.currentTimeMillis ();
 //         System.out.println ( "into the Map Gson running time:" + (endTimeGson - startTimeGson) + "MS");        
 //         
//        long startTimeGson2 = System.currentTimeMillis();
//        Map<Object, Object> ma2 = gson.fromJson(jsonStr, Map.class);
//        long endTimeGson2 = System.currentTimeMillis();
//        System.out.println("Gson转为map运行时间: " + (endTimeGson2 - startTimeGson2) + "ms");
//        
//        System.out.println("........com.alibaba.fastjson..........");
//        
//        
//        long startTimeFastJson = System.currentTimeMillis();
//        com.alibaba.fastjson.JSONObject jsonObject = (com.alibaba.fastjson.JSONObject) com.alibaba.fastjson.JSONObject
//                .toJSON(map);
//        endTimeFastJson = System.currentTimeMillis Long ();
 //         System.out.println ( "com.alibaba.fastjson map into running time:" + (endTimeFastJson - startTimeFastJson) + "MS");
 //         
//         Long startTimeFastJson2 = System .currentTimeMillis ();
 //         String alijson jsonObject.toJSONString = ();
 //         the Map alimap = com.alibaba.fastjson.JSON.parseObject (alijson, Map.class);
 //         Long endTimeFastJson2 = System.currentTimeMillis ();
 / /         System.out.println ( "the Map into com.alibaba.fastjson running time:" + (endTimeFastJson2 - startTimeFastJson2) + "MS"); 
        
        
        // memory footprint 
        System.out.println ( "....... ..used internal memory........." );
        System.out.println ( "......... ......... net.sf.json" ); 
        the Runtime R & lt = Runtime.getRuntime (); 
        r.gc (); 
        Long startMem r.freeMemory = (); // remaining memory at the start of 
        net.sf.json.JSONObject Jo2 = net.sf.json.JSONObject.fromObject (Map);
         Long orz = startMem - r.freeMemory (); // remaining memory now         
        System.out.println ( "map memory for transfer net.sf.json.JSONObject" + orz); 
        
        String S = jo2.toString (); 
        r.gc (); 
        Long startMem2 r.freeMemory = (); / / remaining memory at the start of
        net.sf.json.JSONObject jjo = net.sf.json.JSONObject.fromObject(s);
        Map<Object, Object> m = jjo;
//        Map<Object, Object> m2 = jo2;
        long orz2 = startMem2 - r.freeMemory(); // 剩余内存 现在        
        System.out.println("net.sf.json.JSONObject转map占用内存" + orz2);
        
        System.out.println("........org.json..........");
        r.gc();
        long startMemOrg = r.freeMemory();
        org.json.JSONObject js2 = new org.json.JSONObject(map);
        long memOrg = startMemOrg - r.freeMemory(); 
        System.out.println("org.json.JSONObject转为map占用内存: " + memOrg);
        
        String ss = js2.toString();
        r.gc();
        long startMemOrg2 = r.freeMemory();
        org.json.JSONObject jss2 = new org.json.JSONObject(ss);
        Map<Object, Object> maj = new HashMap();
        @SuppressWarnings("rawtypes")
        Iterator it2 = jss2.keys();
        while (it2.hasNext()) {
            String key = (String) it2.next();
            Object value = jss2.get(key);
            maj.put(key, value.toString());
        }
        long memOrg2 = startMemOrg2 - r.freeMemory(); 
        System.out.println("map转为org.json.JSONObject占用内存: " + memOrg2);
        
        System.out.println("........Gson..........");
        r.gc();
        long startMemGson = r.freeMemory();
        Gson gson2 = new Gson();
        String jsonStr2 = gson2.toJson(map);        
        long memGson = startMemGson - r.freeMemory(); 
        System.out.println("map转为Gson占用内存: " + memGson);    
        
        r.gc();
        long startMemGson2 = r.freeMemory();
        Map<Object, Object> ma22 = gson2.fromJson(jsonStr2, Map.class);
        long memGson2 = startMemGson2 - r.freeMemory(); 
        System.out.println("Gson转为map占用内存: " + memGson2);    
        
        System.out.println("........com.alibaba.fastjson..........");    
        
        r.gc();
        long startMemFastJson = r.freeMemory();
        com.alibaba.fastjson.JSONObject jsonObject2 = (com.alibaba.fastjson.JSONObject) com.alibaba.fastjson.JSONObject
                .toJSON (the Map); 
        Long memFastJson = startMemFastJson -r.freeMemory (); 
        System.out.println ( "com.alibaba.fastjson into memory for the Map:" + memFastJson); 
        
        
        String alijson2 = jsonObject2.toJSONString (); 
        r.gc (); 
        Long startMemFastJson2 = r.freeMemory (); 
        the map alimap2 = com.alibaba.fastjson.JSON.parseObject (alijson2, the map. class );
         Long memFastJson2 = startMemFastJson2 - r.freeMemory (); 
        System.out.println ( "com.alibaba.fastjson into occupied map memory: "+ memFastJson2); 

    
    } 

    // net.sf.json package 
    public  static  void map2jsonstr1(Map<String, Object> map) {

        net.sf.json.JSONObject jo = net.sf.json.JSONObject.fromObject(map);

        Map<Object, Object> m = jo;

    }

    // org.json包
    public static void map2jsonstr2(Map<String, Object> map) {

        org.json.JSONObject js = new org.json.JSONObject(map);

        Map<Object, Object> ma = new HashMap();
        @SuppressWarnings("rawtypes")
        Iterator it = js.keys();
        while (it.hasNext()) {
            String key = (String) it.next();
            Object value = js.get(key);
            ma.put(key, value.toString());
        }

    }

    // com.google.gson包
    public static void map2jsonstr3(Map<String, Object> map) {

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

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

    }

    // com.alibaba.fastjson包
    public static void map2jsonstr4(Map<String, Object> map) {

        com.alibaba.fastjson.JSONObject jsonObject = (com.alibaba.fastjson.JSONObject) com.alibaba.fastjson.JSONObject
                .toJSON(map);
        String alijson = jsonObject.toJSONString();

        Map alimap = com.alibaba.fastjson.JSON.parseObject(alijson, Map.class);

    }

}

 

Guess you like

Origin www.cnblogs.com/happyjia/p/11239781.html