一.有时需要json数据的key全部转成大写或者转成小写
二.直接上代码,一封装成一个工具类了,直接传入JSONObject或JSONArray对象,调用数组或对象的方法就能实现.
package cn.wisesign.commonutil.jsonpath;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Iterator;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
/**
-
修改JSON的key为小写或者大写
*/
@SuppressWarnings(“rawtypes”)
public class JSONKeyTransfer {public static final String JSONOBJECT_KEY = “JSONObject”;
public static final String JSONARRAY_KEY = “JSONArray”;
/**
- 修改json的key为大写或者小写
- @throws
*/
public static JSONObject transferJsonKey(JSONObject json) {
JSONObject object = new JSONObject();
if (json.getClass().toGenericString().endsWith(JSONOBJECT_KEY)) {
JSONObject jsonObject = (JSONObject)json;
Iterator iterator = jsonObject.keys();
while (iterator.hasNext()) {
String jsonKey = (String) iterator.next();
Object valueObject = jsonObject.get(jsonKey);
if (valueObject.getClass().toString().endsWith(JSONOBJECT_KEY)) {
JSONObject checkObject = JSONObject.fromObject(valueObject);
// 当值为null时,valueObject还是JSONObject对象,判空是不成立的,要判断是否是nullObject
if (!checkObject.isNullObject()) {
object.accumulate(jsonKey.toUpperCase(), transferJsonKey((JSONObject) valueObject));
} else {
object.accumulate(jsonKey, null);
}
} else if (valueObject.getClass().toString().endsWith(JSONARRAY_KEY)) {
object.accumulate(jsonKey.toUpperCase(), transferJsonArray(jsonObject.getJSONArray(jsonKey)));
}else{
object.accumulate(jsonKey.toUpperCase(), valueObject);
}
}
}
return object;
}
/**
- JSONArray key大小写转换
- @throws
*/
public static JSONArray transferJsonArray(JSONArray jsonArray) {
JSONArray array = new JSONArray();
if (null != jsonArray && jsonArray.size() > 0) {
for (Object object : jsonArray) {
if (object.getClass().toString().endsWith(JSONOBJECT_KEY)) {
array.add(transferJsonKey((JSONObject) object));
} else if (object.getClass().toString().endsWith(JSONARRAY_KEY)) {
array.add(transferJsonArray((JSONArray) object));
}
}
}
return array;
}
public static void main(String[] args) {
String jsonStr = “”;
try {
File jsonFile = new File(“d:\test.json”);
FileReader fileReader = new FileReader(jsonFile);Reader reader = new InputStreamReader(new FileInputStream(jsonFile),"utf-8"); int ch = 0; StringBuffer sb = new StringBuffer(); while ((ch = reader.read()) != -1) { sb.append((char) ch); } fileReader.close(); reader.close(); jsonStr = sb.toString();
// jsonStr = jsonStr.toUpperCase();
}catch(Exception e){
}
//String str = "{\"contextDTO\":{\"appPackAccToken\":null,\"cAllChainId\":\"\",\"callCount\":0,\"currentCallId\":\"0\",\"employeeId\":\"00000000-0000-0000-0000-000000000000\",\"id\":null,\"latitude\":null,\"loginCurrentCulture\":0,\"loginDepartment\":\"00000000-0000-0000-0000-000000000000\",\"loginDepartmentName\":null,\"loginIP\":null,\"loginOrg\":\"00000000-0000-0000-0000-000000000000\",\"loginTenantId\":\"00000000-0000-0000-0000-000000000000\",\"loginTenantName\":null,\",\"loginUserCode\":\"dddddd\",\"loginUserID\":\"79694a50-ea5f-4d66-99ef-bc7661cc2477\",\"loginUserName\":\"测试\",\"longitude\":null,\"sessionID\":\"97571283-6eed-4fd5-9d7c-6c60dbd362c9\"},\"statusCode\":null}";
JSONObject object = JSONObject.fromObject(jsonStr);
//JSONArray object = JSONArray.fromObject(jsonStr);
object = transferJsonKey(object);
//object = transferJsonArray(object);
System.out.println(object.toString());
}
}