使用的是Fastjson,需求RT
上代码
public static void main(String[] args) {
JSONObject jsonObject = JSON.parseObject("{}");
jsonObject.put("abc", "asd");
jsonObject.put("efg", "");
jsonObject.put("xyz", "efg");
String s = jsonObject.toJSONString();
// 输出原JSON串
System.out.println(s);
String regexp = "\\\"([^\\\"]*)\\\":";
Pattern pattern = Pattern.compile(regexp);
Matcher m = pattern.matcher(s);
StringBuffer sb = new StringBuffer();
while (m.find()) {
m.appendReplacement(sb, m.group().toUpperCase());
}
m.appendTail(sb);
// 输出替换之后的JSON串
System.out.println(sb.toString());
}
结果,JSON的key已全部转为大写:
{"abc":"asd","efg":"","xyz":"efg"}
{"ABC":"asd","EFG":"","XYZ":"efg"}