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

BillyW 2010-04-28 09:40:28
求一个正则表达式:

要求把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"}]



感谢正则达人了~~~~
...全文
1462 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
王集鹄 2010-04-28
  • 打赏
  • 举报
回复
原来String就有这个方法:
public class Main {
public static void main(String[] args) {
String json = "{\"name\":\"value\"}";
String t = json.replaceAll("\"(\\w+)\"(\\s*:\\s*)", "$1$2");
System.out.println(t);
}
}
王集鹄 2010-04-28
  • 打赏
  • 举报
回复
多了几个/
Pattern p = Pattern.compile("\"(\\w+)\"(\\s*:\\s*)");
BillyW 2010-04-28
  • 打赏
  • 举报
回复
通过了。谢谢zswang,我的代码刚才有bug。

结贴
BillyW 2010-04-28
  • 打赏
  • 举报
回复
这样写结果不正确,错误的结果是这样的:
{"{\"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:"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:"2"},children:[],data:"name2",state:"open"}}




[Quote=引用 5 楼 zswang 的回复:]
正在学习java。

Java code
import java.util.regex.*;

public class Main {
public static void main(String[] args) {
String json = "{name:\"value\"}";
Pattern p = Pattern.compile("……
[/Quote]
shan1119 2010-04-28
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 shan1119 的回复:]
str=str.replaceAll("\\"([^\\"]+?)\\"[:]","\\1");
[/Quote]
不対,是 str=str.replaceAll("\\"([^\\"]+?)\\"[:]","$1");
shan1119 2010-04-28
  • 打赏
  • 举报
回复
str=str.replaceAll("\\"([^\\"]+?)\\"[:]","\\1");
BillyW 2010-04-28
  • 打赏
  • 举报
回复
如果在java服务层代码里写,应该怎么写呢?
王集鹄 2010-04-28
  • 打赏
  • 举报
回复
正在学习java。
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);
}
}
BillyW 2010-04-28
  • 打赏
  • 举报
回复
感谢zswang

如果在java服务层代码里写,应该怎么做呢?
Jeffrey84 2010-04-28
  • 打赏
  • 举报
回复
1楼的就可以了
王集鹄 2010-04-28
  • 打赏
  • 举报
回复
兼容一下有空格的情况:
str = str.replace(/"(\w+)"(\s*:\s*)/g, "$1$2");

推荐正则调试工具:http://www.renrousousuo.com/tools/regex_debug.html
王集鹄 2010-04-28
  • 打赏
  • 举报
回复
str = str.replace(/"(\w+)":/g, "$1:");

87,910

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧