首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > JavaScript >

【求正则】要求把json串中“:”前面名称部分的双引号去掉,值中的双引号保留,该如何处理

2012-02-16 
【求正则】要求把json串中“:”前面名称部分的双引号去掉,值中的双引号保留求一个正则表达式:要求把json串中“:

【求正则】要求把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);    }} 

热点排行