json字符串还原map

今天有个需求,由于调用第三方接口一直调不通,但是又需要先把接口写好供前端调用,于是想到以前写的一个json字符串还原的类。这里做个笔记。

package com.hz.token;

import com.google.gson.Gson;

import java.util.List;
import java.util.Map;

/**
 * 2017/6/26.
 */
public class JsonToStringUtil {
    private static StringBuffer sb = new StringBuffer("Map jsonMap = new HashMap();\n");

    private JsonToStringUtil() {

    }


    public static void main(String[] args) {
        String s = parseJsonStr(CONTENT);
        System.out.println(s);
    }


    public static String parseJsonStr(String jsonStr) {
        Gson gson = new Gson();
        Map<String, Object> map = gson.fromJson(jsonStr, Map.class);
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            Object value = entry.getValue();
            if (value instanceof String) {
                appendStringMap("jsonMap", entry.getKey(), (String) value);
            }
            if (value instanceof Number) {
                appendObjectMap("jsonMap", entry.getKey(), value);
            }
            if (value instanceof Map) {
                appendMapMap(entry.getKey(), (Map) value);
                appendObjectMap("jsonMap", entry.getKey(), entry.getKey());
            }
            if (value instanceof List) {
                appendListMap(entry.getKey(), (List) value);
                appendObjectMap("jsonMap", entry.getKey(), entry.getKey());
            }

        }
        return sb.toString();
    }

    private static void appendStringMap(String mapKey, String key, String value) {
        sb.append(mapKey).append(".put(\"").append(key).append("\",\"").append(value).append("\");\n");
    }

    private static void appendObjectMap(String mapKey, String key, Object value) {
        sb.append(mapKey).append(".put(\"").append(key).append("\",").append(value).append(");\n");
    }

    private static void appendMapMap(String subMapKey, Map<String, Object> subMapValue) {
        sb.append("Map ").append(subMapKey).append("= new HashMap();\n");
        for (Map.Entry<String, Object> entry : subMapValue.entrySet()) {
            Object value = entry.getValue();
            if (value instanceof String) {
                appendStringMap(subMapKey, entry.getKey(), (String) value);
            }
            if (value instanceof Number) {
                appendObjectMap(subMapKey, entry.getKey(), value);
            }
            if (value instanceof Map) {
                appendMapMap(entry.getKey(), (Map) value);
                appendObjectMap(subMapKey, entry.getKey(), entry.getKey());
            }
            if (value instanceof List) {
                appendListMap(entry.getKey(), (List) value);
                appendObjectMap(subMapKey, entry.getKey(), entry.getKey());
            }
        }
    }

    private static void appendStringList(String listKey, Object value) {
        sb.append(listKey).append(".add(\"").append(value).append("\");\n");
    }

    private static void appendObjectList(String listKey, Object value) {
        sb.append(listKey).append(".add(").append(value).append(");\n");
    }

    private static void appendListMap(String subListKey, List subListValue) {
        sb.append("List ").append(subListKey).append("= new ArrayList();\n");
        int i = 1;
        for (Object value : subListValue) {
            if (value instanceof String) {
                appendStringList(subListKey, value);
            }
            if (value instanceof Number) {
                appendObjectList(subListKey, value);
            }
            if (value instanceof Map) {
                String mapName = subListKey + "SubMap" + i;
                appendMapMap(mapName, (Map) value);
                appendObjectList(subListKey, mapName);
            }
            if (value instanceof List) {
                String listName = subListKey + "SubList" + i;
                appendListMap(listName, (List) value);
                appendObjectList(subListKey, listName);
            }
            i++;
        }
    }

    private static String CONTENT = "{\"authCode\":\"drivingLicense\",\"childCommitType\":\"drivingLicenseInfos\",\"custNo\":\"955628\",\"loanCode\":\"carat\",\"mainAuthlog\":\"2016-07-01\",\"mainCardno\":\"沪A12345\",\"mainMotorno\":\"HJ145632\",\"mainRegistdate\":\"2016-07-01\",\"ssAccountId\":\"\"}\n";
}
image.png

关注我的公众号,都是满满的干货!

孙坚.gif
上一篇:《Linux防火墙(第4版)》——2.2 选择一个默认的数据包过滤策略


下一篇:使用微信你需要了解的五个小技巧