private JSONObject covertObject(JSONObject object) {
if (object == null) {
return null;
}
JSONObject newObject = new JSONObject();
Set<String> set = object.keySet();
for (String key : set) {
Object value = object.get(key);
if (value instanceof JSONArray) {
//数组
value = covertArray(object.getJSONArray(key));
} else if (value instanceof JSONObject) {
//对象
value = covertObject(object.getJSONObject(key));
}
//这个方法自己写的改成驼峰,也可以改成大写小写
key = lineToHump(key);
newObject.put(key, value);
}
return newObject;
}
private JSONArray covertArray(JSONArray array) {
if (array == null) {
return null;
}
JSONArray newArray = new JSONArray();
for (int i = 0; i < array.size(); i++) {
Object value = array.get(i);
if (value instanceof JSONArray) {
//数组
value = covertArray(array.getJSONArray(i));
} else if (value instanceof JSONObject) {
//对象
value = covertObject(array.getJSONObject(i));
}
newArray.add(value);
}
return newArray;
}
public static String lineToHump(String str) {
str = str.toLowerCase();
Pattern linePattern = Pattern.compile("_(\\w)");
Matcher matcher = linePattern.matcher(str);
StringBuffer sb = new StringBuffer();
while (matcher.find()) {
matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
}
matcher.appendTail(sb);
return sb.toString();
}
调用测试
@RestController
@RequestMapping("/test")
public class TestController {
@Autowired
private TestService testService;
@RequestMapping(value="/quary")
public Object TestCon(){
JSONObject object = new JSONObject();
object.put("COMM_ID",""); //commId
object.put("COMM_ID_NAME",""); //commIdName
object.put("eparchyname",""); //eparchyname
return testService.covertObject(object);
}
}