当前位置: 首页 > 面试题库 >

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

范鸿
2023-03-14
问题内容

我正在尝试使用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\"}]"

现在,如果您仔细查看字符串,您会发现它包含一个 \" 而不是every "
。该字符串目前无法格式化为JSONArray。所以,我需要更换的每个实例 \"" ,这将有一个非常简单的任务,也
\ 没有一个 转义序列

我尝试使用以下代码。

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):  ^

我的整个代码,以防万一:

public void getAllDealsFromServerJson()
{

    String apiUrl = "http://passme.azurewebsites.net/api/TestApi/";


    HttpClient client = new DefaultHttpClient();
    HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); //Timeout Limit
    HttpResponse response;
    JSONObject json = new JSONObject();

    try{
        HttpPost httpPost = new HttpPost(apiUrl);
        json.put("requestType", "getalldeals" );

        StringEntity se = new StringEntity( json.toString());  
        se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
        httpPost.setEntity(se);
        response = client.execute(httpPost);
        Log.d("Http Response:", response.toString());
        jsonResponse = response.toString();

        BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
        String jsonStr = reader.readLine();
        Log.d("String Response", jsonStr);
        String jsonFormattedString = jsonStr.replaceAll("\\", ""); // gives error
        Log.d("Formatted String", jsonFormattedString);
        //JSONTokener tokener = new JSONTokener(jsonFormattedString);
        /*JSONObject finalResult = new JSONObject(tokener);
        Log.d("JSON Response", "" + finalResult.optString("Title"));*/
        JSONArray resultArray = new JSONArray(jsonFormattedString);
        Log.d("JSON Array Result Length", "" + resultArray.length());
        Log.d("JSON Array Result ", "" + resultArray.getJSONObject(0).optInt("DealId"));

    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
}

问题答案:

试试这个:

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

因为反斜杠是正则表达式中的转义字符(将replaceAll()一个作为参数接收),所以它也必须转义。



 类似资料:
  • 问题内容: 我可以用这个: 有没有办法从Java中的字符串中删除所有出现的字符? 我尝试了这个,不是我想要的: 问题答案: 尝试使用需要参数(例如)而不是的重载:

  • 问题内容: 我面临网址问题,我希望能够转换标题,该标题可以包含任何内容,并去除所有特殊字符,因此它们仅包含字母和数字,当然我想用连字符替换空格。 怎么做?我听说过很多关于正则表达式(regex)的使用… 问题答案: 这应该可以满足您的需求: 用法: 将输出: 编辑: 嘿,只是一个简单的问题,如何防止多个连字符彼此相邻?并将它们替换为1?

  • 如何从R中的字符串中删除所有特殊字符并用空格替换它们? 要删除的一些特殊字符是:

  • 问题内容: 我正在编写python MapReduce字数统计程序。问题是数据中散布着许多非字母字符,我发现这篇文章从Python的字符串中剥离了除了字母数字字符之外的所有内容,这显示了使用正则表达式的一个很好的解决方案,但是我不确定如何实现它 恐怕我不确定该如何使用该库甚至正则表达式。我不确定如何将正则表达式模式正确地应用于传入的字符串(书的一行)以检索没有任何非字母数字字符的新行。 有什么建议

  • 问题内容: 我在textarea中有一个文本,我使用.value属性将其读出。 现在,我想使用正则表达式.replace从我的文本中删除所有换行符(按时产生的字符),但是如何在正则表达式中指示换行符? 如果那不可能,还有另一种方法吗? 问题答案: 这可能是一个常见问题解答。无论如何,换行符(更好的是:换行符)可以是回车符(在较旧的Mac上为CR,),换行符(在Unices 包括Linux上为LF,

  • 问题内容: 如何从GoLang中的UTF-8字符串中删除所有Unicode换行符?我为PHP找到了这个答案 问题答案: 您可以使用: