package com.cyhl.cs.util;
import jdk.nashorn.internal.runtime.regexp.joni.encoding.CharacterType;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import java.util.Iterator;
/**
* @author gaojh
* @version 1.0.0
* @ClassName Test.java
* @Description TODO
*/
public class Test {
private static String STRING_CLAZZ = "String";
private static String JSONOBJECT_CLAZZ = "JSONObject";
private static String JSONARRAY_CLAZZ = "JSONArray";
public static String INITIAL_SCHEMA = "initial"; //首字母
public static String ALL_SCHEMA = "all"; //所有
/**
* @throws
* @title transformJsonKey
* @description JsonKey 转换工具
* @author gaojh
* @param: jsonObject
* @param: transition 转换类型
* @param: pattern 转换模式
* @return: net.sf.json.JSONObject
*/
public static JSONObject transformJsonKey(JSONObject jsonObject, Integer transition, String pattern) {
//验空判断
if (null == jsonObject || null == transition || StringUtils.isBlank(pattern)) {
return null;
}
JSONObject jsonTemp = new JSONObject();
Iterator iterator = jsonObject.keys();
while (iterator.hasNext()) {
String jsonKey = (String) iterator.next();
Object valueObject = jsonObject.get(jsonKey);
//转换key
String sourceKey = transform(jsonKey, transition, pattern);
if (valueObject.getClass().toString().endsWith(STRING_CLAZZ)) {
jsonTemp.accumulate(sourceKey, valueObject);
} else if (valueObject.getClass().toString().endsWith(JSONOBJECT_CLAZZ)) {//对象套对象
JSONObject checkObject = JSONObject.fromObject(valueObject);
if (!checkObject.isEmpty()) {//json对象进行递归调用 返回json对象
jsonTemp.accumulate(sourceKey, transformJsonKey((JSONObject) valueObject, transition, pattern));
} else {//空对象直接 赋值
jsonTemp.accumulate(sourceKey, "{}");
}
} else if (valueObject.getClass().toString().endsWith(JSONARRAY_CLAZZ)) {//对象套数组
jsonTemp.accumulate(sourceKey, transformJsonArray(jsonObject.getJSONArray(jsonKey), transition, pattern));
}
}
return jsonTemp;
}
/**
* @throws
* @title transformJsonArray
* @description JsonKey 转换工具
* @author gaojh
* @param: jsonArray
* @param: transition 转换类型
* @param: pattern 转换模式
* @return: net.sf.json.JSONArray
*/
public static JSONArray transformJsonArray(JSONArray jsonArray, Integer transition, String pattern) {
JSONArray arrayTemp = new JSONArray();
if (null != jsonArray && jsonArray.size() > 0) {
for (Object object : jsonArray) {
if (object.getClass().toString().endsWith(JSONOBJECT_CLAZZ)) {
arrayTemp.add(transformJsonKey((JSONObject) object, transition, pattern));
} else if (object.getClass().toString().endsWith(JSONARRAY_CLAZZ)) {
arrayTemp.add(transformJsonArray((JSONArray) object, transition, pattern));
}
}
}
return arrayTemp;
}
private static String transform(String jsonKey, Integer transition, String pattern) {
String sourceKey = "";
if (CharacterType.LOWER == transition) { //小写
if (INITIAL_SCHEMA.equals(pattern)) {
String start = jsonKey.substring(0, 1).toLowerCase();
String end = jsonKey.substring(1);
sourceKey = start + end;
} else if (ALL_SCHEMA.equals(pattern)) {
sourceKey = jsonKey.toLowerCase();
} else {
System.out.printf("ERROR - 无效转换类型");
throw new RuntimeException();
}
} else if (CharacterType.UPPER == transition) {//大写
if (INITIAL_SCHEMA.equals(pattern)) {
String start = jsonKey.substring(0, 1).toUpperCase();
String end = jsonKey.substring(1);
sourceKey = start + end;
} else if (ALL_SCHEMA.equals(pattern)) {
sourceKey = jsonKey.toUpperCase();
} else {
System.out.printf("ERROR - 无效转换类型");
throw new RuntimeException();
}
} else {
System.out.printf("ERROR - 无效转换模式");
throw new RuntimeException();
}
return sourceKey;
}
public static void main(String[] args) throws Exception {
JSONObject j1 = new JSONObject();
j1.put("UserName", "1");
j1.put("PAssWord", "2");
j1.put("ifNull", "3");
JSONObject j2 = new JSONObject();
j2.put("UserName", "1");
j2.put("PAssWord", "2");
j2.put("ifNull", "3");
JSONObject j3 = new JSONObject();
j3.put("UserName", "1");
j3.put("PAssWord", "2");
j3.put("ifNull", "3");
JSONArray a1 = new JSONArray();
a1.add(new JSONObject());
a1.add(j3);
a1.add(new JSONObject());
JSONObject jsonObject = new JSONObject();
jsonObject.put("UserName", "1");
jsonObject.put("PAssWord", "2");
jsonObject.put("ifNull", "3");
jsonObject.put("j0", new JSONObject());
jsonObject.put("J1", j1);
jsonObject.put("j2", j2);
jsonObject.put("A1", a1);
System.out.println("数据源 " + jsonObject.toString());
//转小写 首字母模式
JSONObject jsonObject1 = transformJsonKey(jsonObject, CharacterType.LOWER, Test.INITIAL_SCHEMA);
System.out.println("转小写 首字母模式 " + jsonObject1.toString());
//转大写 全字母模式
JSONObject jsonObject2 = transformJsonKey(jsonObject, CharacterType.UPPER, Test.ALL_SCHEMA);
System.out.println("转大写 全字母模式 " + jsonObject2.toString());
}
}
人工手写,亲测可用。(注:嵌套数组可能出现问题请自行修改)