JSON数据的keg值转换,标准驼峰和不标准驼峰转下划线等6种方式转换

package com.beeasy.loadqcc.utils;

import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Set;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class JsonConvertUtils {

//标准驼峰和不标准驼峰转下划线
public static String convert1(String json) {
    JSONObject jsonObject = JSON.parseObject(json);
    return convert1(jsonObject).toJSONString();
}
private static JSONObject convert1(JSONObject input) {
    JSONObject result = new JSONObject();
    Set> entrySet = input.entrySet();
    for (Iterator> it = entrySet.iterator(); it.hasNext();) {
        Entry entry = it.next();
        Object obj = entry.getValue();
        String key = entry.getKey();
        String[] ss = key.split("(?> entrySet = input.entrySet();
    for (Iterator> it = entrySet.iterator(); it.hasNext();) {
        Entry entry = it.next();
        Object obj = entry.getValue();
        String key = entry.getKey();
        StringBuffer sb=new StringBuffer();
        sb.append(toLowerCaseFirstOne(key));

        if(obj instanceof JSONObject) {
            result.put(sb.toString(), convert2((JSONObject)obj));
        }else if(obj instanceof JSONArray) {
            result.put(sb.toString(), convert2((JSONArray)obj));
        }else {
            result.put(sb.toString(), entry.getValue());
        }
    }
    return result;
}
private static JSONArray convert2(JSONArray input) {
    JSONArray result = new JSONArray();
    for(int i=0;i> entrySet = input.entrySet();
    for (Iterator> it = entrySet.iterator(); it.hasNext();) {
        Entry entry = it.next();
        Object obj = entry.getValue();
        String key = entry.getKey();

        if(obj instanceof JSONObject) {
            result.put(sb.toString(), convert3((JSONObject)obj));
        }else if(obj instanceof JSONArray) {
            result.put(sb.toString(), convert3((JSONArray)obj));
        }else {
            result.put(sb.toString(), entry.getValue());
        }


    }
    return result;
}
private static JSONArray convert3(JSONArray input) {
    JSONArray result = new JSONArray();
    for(int i=0;i keys = jo.keySet();
        String[] array = keys.toArray(new String[keys.size()]);
        for (String key : array) {
            Object value = jo.get(key);
            String[] key_strs = key.split("_");
            if (key_strs.length > 1) {
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < key_strs.length; i++) {
                    String ks = key_strs[i];
                    if (!"".equals(ks)) {
                        if (i == 0) {
                            sb.append(ks);
                        } else {
                            int c = ks.charAt(0);
                            if (c >= 97 && c <= 122) {
                                int v = c - 32;
                                sb.append((char) v);
                                if (ks.length() > 1) {
                                    sb.append(ks.substring(1));
                                }
                            } else {
                                sb.append(ks);
                            }
                        }
                    }
                }
                jo.remove(key);
                jo.put(sb.toString(), value);
            }
            convert(value);
        }
    }
}
public final static Object convert(String json) {
    Object obj = JSON.parse(json);
    convert(obj);
    return obj;
}


//下滑线转不标准驼峰
public final static void convert4(Object json) {
    if (json instanceof JSONArray) {
        JSONArray arr = (JSONArray) json;
        for (Object obj : arr) {
            convert4(obj);
        }
    } else if (json instanceof JSONObject) {
        JSONObject jo = (JSONObject) json;
        Set keys = jo.keySet();
        String[] array = keys.toArray(new String[keys.size()]);
        for (String key : array) {
            Object value = jo.get(key);
            String[] key_strs = key.split("_");
            if (key_strs.length > 1) {
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < key_strs.length; i++) {
                    String ks = key_strs[i];
                    if (!"".equals(ks)) {
                        if (i == 0) {
                            sb.append(toUpperCaseFirstOne(ks));
                        } else {
                            int c = ks.charAt(0);
                            if (c >= 97 && c <= 122) {
                                int v = c - 32;
                                sb.append((char) v);
                                if (ks.length() > 1) {
                                    sb.append(ks.substring(1));
                                }
                            } else {
                                sb.append(ks);
                            }
                        }
                    }
                }
                jo.remove(key);
                jo.put(sb.toString(), value);
            }
            convert4(value);
        }
    }
}
public final static Object convert4(String json) {
    Object obj = JSON.parse(json);
    convert4(obj);
    return obj;
}

}

你可能感兴趣的:(json)