【求正则】要求把json串中“:”前面名称部分的双引号去掉,值中的双引号保留
求一个正则表达式:
要求把json串中“:”前面名称部分的双引号去掉,值中的双引号保留。如下例:
原字符串:
[{"attributes":{"id":"1"},"children":[{"attributes":{"id":"11"},"children":[],"data":"name11","state":"open"},{"attributes":{"id":"12"},"children":[],"data":"name12","state":"open"}],"data":"name1","state":"open"},{"attributes":{"id":"2"},"children":[],"data":"name2","state":"open"}]
我需要的字符串:(注意“冒号”前面名称部分的引号没有了,值中的引号是保留的。
[{attributes:{id:"1"},children:[{attributes:{id:"11"},children:[],data:"name11",state:"open"},{attributes:{id:"12"},children:[],data:"name12",state:"open"}],data:"name1",state:"open"},{attributes:{id:"2"},children:[],data:"name2",state:"open"}]
感谢正则达人了~~~~
------解决方案--------------------
正在学习java。
Java code
import java.util.regex.*;
public class Main {
public static void main(String[] args) {
String json = "{name:\"value\"}";
Pattern p = Pattern.compile("/\"(\\w+)\"(\\s*:/\\s*)");
Matcher m = p.matcher(json);
String t = m.replaceAll("$1$2");
System.out.println(t);
}
}