json字符串中key值下划线命名转换为驼峰命名:
例如:
原json串:
String json= "{'user_name':'ok','user_sex':0,'object_info':{'business_code':'0001','business_info':{'business_name':'ok'}}}";
转换为:
String json= "{'userName':'ok','userSex':0,'objectInfo':{'businessCode':'0001','businessInfo':{'businessName':'ok'}}}";
具体工具类如下:
public class JsonUtils { public final static void convert(Object json) {
if (json instanceof JSONArray) {
JSONArray arr = (JSONArray) json;
for (Object obj : arr) {
convert(obj);
}
} else if (json instanceof JSONObject) {
JSONObject jo = (JSONObject) json;
Set<String> 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;
}
}