从字符串中删除所有出现的\

时间:2012-12-18 19:17:31

标签: java regex string replace escaping

我正在尝试使用JSON从服务器获取一组对象。

服务器向我发送以下字符串。

"[{\"DealComment\":null,\"DealVotes\":[],\"DealId\":1,\"CompanyId\":1,\"StartDate\":\"2012-12-13T00:00:00\",\"EndDate\":\"2012-12-16T00:00:00\",\"CouponCode\":\"Test Coupon 1\",\"Description\":\"Test Deal Description 1\",\"VoteUp\":null,\"VoteDown\":null,\"ViewCount\":null,\"Title\":\"Test Deal 1\"},{\"DealComment\":null,\"DealVotes\":[],\"DealId\":2,\"CompanyId\":1,\"StartDate\":\"2012-12-16T00:00:00\",\"EndDate\":\"2012-12-17T00:00:00\",\"CouponCode\":\"Test Coupon 2\",\"Description\":\"Test Description 2\",\"VoteUp\":null,\"VoteDown\":null,\"ViewCount\":null,\"Title\":\"Test Deal 2\"},{\"DealComment\":null,\"DealVotes\":[],\"DealId\":3,\"CompanyId\":1,\"StartDate\":\"2012-12-14T00:00:00\",\"EndDate\":\"2012-12-15T00:00:00\",\"CouponCode\":\"Test Code 3\",\"Description\":\"Test Description 3\",\"VoteUp\":null,\"VoteDown\":null,\"ViewCount\":null,\"Title\":\"Test Deal 3\"},{\"DealComment\":null,\"DealVotes\":[],\"DealId\":4,\"CompanyId\":1,\"StartDate\":\"2012-12-12T00:00:00\",\"EndDate\":\"2012-12-13T00:00:00\",\"CouponCode\":\"Test Coupon 4\",\"Description\":\"Test Description 4\",\"VoteUp\":null,\"VoteDown\":null,\"ViewCount\":null,\"Title\":\"Test Deal 4\"},{\"DealComment\":null,\"DealVotes\":[],\"DealId\":5,\"CompanyId\":2,\"StartDate\":\"2012-12-12T00:00:00\",\"EndDate\":\"2012-12-14T00:00:00\",\"CouponCode\":\"AwD\",\"Description\":\"Very awesome deal!\",\"VoteUp\":null,\"VoteDown\":null,\"ViewCount\":null,\"Title\":\"Awesome Deal 1\"}]"

现在,如果仔细查看字符串,您会注意到它包含 \" ,而不是每个 " 。该字符串现在无法格式化为JSONArray。所以,我需要用 \" 替换每次出现的 " ,这将是一项非常简单的任务, {{1 }} 不是转义序列

我尝试使用以下代码。

\

但它给了我以下例外。

String jsonFormattedString = jsonStr.replaceAll("\\", "");

我的整个代码,如果有任何用处:

12-19 00:35:59.575: W/System.err(444): java.util.regex.PatternSyntaxException: Syntax error U_REGEX_BAD_ESCAPE_SEQUENCE near index 1:
12-19 00:35:59.575: W/System.err(444): \
12-19 00:35:59.575: W/System.err(444):  ^

5 个答案:

答案 0 :(得分:13)

实际上正确的方法是:

String jsonFormattedString = jsonStr.replace("\\\"", "\"");

您只想将\"替换为",而不是将所有\替换为 nothing (如果您有json字符串,它会占用您的斜杠那些)。 与流行的相信replace(...)相反,也替换了所有出现的给定字符串,就像replaceAll(...)一样,它只是不使用正则表达式,所以它通常更快。

答案 1 :(得分:5)

看起来你的传入字符串是双重JSON编码的。你应该解码它,然后再解码它。

我最好猜测你如何在Java中做到这一点:

JSONArray resultArray = new JSONArray(new JSONString(jsonFormattedString));

我在这里假设JSONString是一种类型。您的实际解决方案可能会有所不同。

在正常情况下,我希望服务能够直接为您提供JSON。看来这个服务给你一个字符串(根据JSON规范编码)包含 JSON。

这是以下两者之间的区别:

String someJSON = "[0, 1, 2]";
String doublyEncodedJSON = "\"[0, 1, 2]\"";

请注意额外的前导和尾随引号?那是因为后者是一串JSON。您必须对其进行两次解码才能获得实际对象。

答案 2 :(得分:3)

你可以使用:

str.replace("\\","");

replace将string作为param,replaceAll使用RegEx。它也可能这样工作:

str.replaceAll("\\\\", "");

答案 3 :(得分:3)

只需使用:

try {
        jsonFormattedString = new JSONTokener(jsonString).nextValue().toString();
    } catch (JSONException e) {
        e.printStackTrace();
    }

参见文档

答案 4 :(得分:0)

jsonObj.toString()
        .replace("\"[", "[").replace("]\"", "]")
        .replace("\\\"{", "{").replace("}\\\"", "}")
        .replace("\\\\\\\"", "\"")