不同json和map互转

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转为net.sf.json.JSONObject运行时间: " + (endTime - startTime) + "ms");
//        
//        long startTime2 = System.currentTimeMillis(); // 获取开始时间
//        Map<Object, Object> m = jo;
//        long endTime2 = System.currentTimeMillis(); // 获取结束时间
//        System.out.println("net.sf.json.JSONObject转为map运行时间: " + (endTime2 - startTime2) + "ms");
//        
//        System.out.println("........org.json..........");
//        long startTimeOrg = System.currentTimeMillis(); // 获取结束时间
//        org.json.JSONObject js = new org.json.JSONObject(map);
//        long endTimeOrg = System.currentTimeMillis(); // 获取结束时间
//        System.out.println("org.json.JSONObject转为map运行时间: " + (endTimeOrg - startTimeOrg) + "ms");
//        
//        long startTimeOrg2 = System.currentTimeMillis(); // 获取开始时间
//        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());
//        }
//        long endTimeOrg2 = System.currentTimeMillis(); // 获取结束时间
//        System.out.println("map转为org.json.JSONObject运行时间: " + (endTimeOrg2 - startTimeOrg2) + "ms");        
//        
//        System.out.println("........Gson..........");
//        long startTimeGson = System.currentTimeMillis();
//        Gson gson = new Gson();
//        String jsonStr = gson.toJson(map);
//        long endTimeGson = System.currentTimeMillis();
//        System.out.println("map转为Gson运行时间: " + (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);
//        long endTimeFastJson = System.currentTimeMillis();
//        System.out.println("com.alibaba.fastjson转为map运行时间: " + (endTimeFastJson - startTimeFastJson) + "ms");
//        
//        long startTimeFastJson2 = System.currentTimeMillis();
//        String alijson = jsonObject.toJSONString();
//        Map alimap = com.alibaba.fastjson.JSON.parseObject(alijson, Map.class);
//        long endTimeFastJson2 = System.currentTimeMillis();
//        System.out.println("map转为com.alibaba.fastjson运行时间: " + (endTimeFastJson2 - startTimeFastJson2) + "ms");
        
        
        //内存占用
        System.out.println(".........占用内存.........");
        System.out.println(".........net.sf.json.........");
        Runtime r = Runtime.getRuntime();
        r.gc();
        long startMem = r.freeMemory(); // 开始时的剩余内存
        net.sf.json.JSONObject jo2 = net.sf.json.JSONObject.fromObject(map);
        long orz = startMem - r.freeMemory(); // 剩余内存 现在        
        System.out.println("map转net.sf.json.JSONObject占用内存" + orz);
        
        String s=jo2.toString();
        r.gc();
        long startMem2 = r.freeMemory(); // 开始时的剩余内存
        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(map);
        long memFastJson = startMemFastJson - r.freeMemory(); 
        System.out.println("map转为com.alibaba.fastjson占用内存: " + memFastJson);
        
        
        String alijson2 = jsonObject2.toJSONString();
        r.gc();
        long startMemFastJson2 = r.freeMemory();
        Map alimap2 = com.alibaba.fastjson.JSON.parseObject(alijson2, Map.class);
        long memFastJson2 = startMemFastJson2 - r.freeMemory();
        System.out.println("com.alibaba.fastjson转为map占用内存: " + memFastJson2);

    
    }

    // net.sf.json包
    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);

    }

}

 

posted @ 2019-07-24 17:57  happy老家  阅读(495)  评论(0编辑  收藏  举报