一个Map<String,Object>转换成JSONARRAY对象,然后toString(),{"test1":1,"test2":"2","test_3":"3","0701":23}需要把这个json串的key转成大写,如何实现?前提:不循环map方式一:
我用((?<=")[\w\d_]*(?=":))可以查找字符串中匹配的key,但是不知道如何继续操作更简便。
使用matcher.replaceAll("$1");这样的方式可以进行更改key大写么?
方式二:
用了String processPropertyName(Class beanClass,
String name),想在转换josn对象的时候一起转化,但是这里的beanClass我不知道应该写什么,使用bean的话还好说,可以用对应的class
我用((?<=")[\w\d_]*(?=":))可以查找字符串中匹配的key,但是不知道如何继续操作更简便。
使用matcher.replaceAll("$1");这样的方式可以进行更改key大写么?
方式二:
用了String processPropertyName(Class beanClass,
String name),想在转换josn对象的时候一起转化,但是这里的beanClass我不知道应该写什么,使用bean的话还好说,可以用对应的class
[code=Java]String test = "{'test12aa':1,'test2':'2','test_3dd':'3,'0701':23}";
Matcher m = Pattern.compile("\\'([a-z0-9_]*)\\':").matcher(test);
//匹配格式: "小写字母数字下划线":
while (m.find()) {
String temp = m.group(1);
test = test.replaceFirst(temp, temp.toUpperCase());
}
System.out.println(test);code]
这是我的测试代码,你看是你所需要的么?
Matcher m = Pattern.compile("\\'([a-z0-9_]*)\\':").matcher(test);
//匹配格式: "小写字母数字下划线":
while (m.find()) {
String temp = m.group(1);
test = test.replaceFirst(temp, temp.toUpperCase());
}
System.out.println(test);
//测试结果输出 {'TEST12AA':1,'TEST2':'2','TEST_3DD':'3,'0701':23}
code没发好
#!/usr/bin/perl
my $str = "{\"test1\":test1,\"test2\":\"test2\",\"test_3\":\"3\",\"0701\":23}";
$str =~ s/(\"[^\"]*?\")(?=\:)/\U\1/g;
print $str,"\n";
但是,java好像不支持预处理操作 \l \u、\L 和 \U。
test = test.replaceFirst(temp, temp.toUpperCase());
这个是有BUG的,不能这么替换
String test = "{'test1':'test2','test2':'2','test_3dd':'3,'0701':23}";
替换了就出错了
String test = "{'test12aa':1,'test2':'2','test_3dd':'3,'0701':23}";
Pattern p = Pattern.compile("\\'([0-9a-z_]*?)\\'\\:");
Matcher m = p.matcher(test);
while(m.find()){
System.out.println(m.group());
}
Matcher m = Pattern.compile("\"([a-z0-9_]*)\":\"(\\w*)\"")
.matcher(test);
// 匹配格式:
// "小写字母数字下划线":"字母数字下划线"
while (m.find()) {
test = test.replaceFirst(m.group(),
new StringBuilder("\"").append(m.group(1).toUpperCase())
.append("\":\"").append(m.group(2)).append("\"")
.toString());
}
System.out.println(test);
// //测试结果输出
// {"TEST2":"test2","TEST2":"2","TEST_3DD":"3","0701":"23"}这样应该行了吧 匹配整个key value 然后只替换key 写的方法比较死 见笑了 还有其他什么场景么 我没写过解析json的东西 不知道还有那些场景
"\"(\\w*)\":\"(\\w*)\"")这样吧,不影响结果只是key的全大写情况也会去做uppercase而已.而且不知道你的value从哪里来,如果value允许字母数字下划线之外的其它字符,这个会匹配不到,不知道你有没有其他的想法.
不是说正则不行,而是我这个表达式不行.
把表达式写成这样 "\"(\\w*)\":\"([^\"]*)\""
匹配格式变成 "字母数字下划线":"非双引号的任意字符"
这样匹配的话就行了,虽然匹配的是非双引号的任意字符,但是就算value出现双引号也不会出错的.
String s="{'test1':'test2','test2':'2','test_3dd':'3','0701':23}";
String reg="'(\\w+)(?=':)";
Pattern p=Pattern.compile(reg);
Matcher m=p.matcher(s);
while(m.find())
{
s=s.replaceFirst(reg, m.group(1).toUpperCase());
}
System.out.println(s);打印结果 {TEST1':'test2',TEST2':'2',TEST_3DD':'3',0701':23}
String s="{'test1':'test2','test2':'2','test_3dd':'3','0701':23}";
String reg="'([a-zA-Z_$]\\w*)(?=':)";
Pattern p=Pattern.compile(reg);
Matcher m=p.matcher(s);
while(m.find())
{
s=s.replaceFirst(reg, m.group(1).toUpperCase());
}
System.out.println(s);
换成这个更合适
我知道你的这个方法的思想了,最开始我看到s=s.replaceFirst(reg, m.group(1).toUpperCase());
还疑惑,这样肯定要出错的,但是看了你的正则,你压根就没想完全匹配key,你匹配['key]
所以你最后替换了大写之后,就匹配不上了,所以就可以继续往后匹配所以你的东西是{TEST1':'test2',TEST2':'2',TEST_3DD':'3',0701':23}
不是正确的json格式也是一个思路,可以修改一下拿来用
StringBuffer sb=new StringBuffer();
String s="{'test1':'test2','test2':'2','test_3dd':'3','0701':23}";
Pattern p=Pattern.compile("'[a-z0-9_]+':");
Matcher m=p.matcher(s);
while(m.find())
m.appendReplacement(sb,m.group().toUpperCase());
m.appendTail(sb);
System.out.println(sb.toString());