String testStr = "2015-10-16 11:23:59,679 INFO [] UserOrderManager:? - placeOrder param:{\"addContent\":\"\",\"areaCode\":\"021\",\"channelId\":\"9001\",\"couponId\":\"\",\"dialectType\":\"100\",\"isAnewAsk\":\"0\",\"orderParentId\":\"\",\"origin\":\"\",\"osType\":\"1\",\"parentProductId\":\"1096\",\"productId\":\"1109\",\"proxyFlag\":\"0\",\"telephone\":\"18888888888\",\"userId\":\"1848\"}";
Pattern p = Pattern.compile("\\{(.*)\\}");
Matcher m = p.matcher(testStr);
StringBuffer buf=new StringBuffer();
m.find();
buf.append("{");
buf.append(m.group(1));
buf.append("}");
System.out.println(buf.toString());
由于"{}"是特殊字符,因此需要使用转义符
本文介绍如何使用Java中的正则表达式从字符串中提取JSON格式的数据,并将其转换为标准的JSON格式。通过实例演示了如何匹配并捕获特定模式的字符串,如本例中的大括号内的内容。
5万+

被折叠的 条评论
为什么被折叠?



