首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >从字符串中删除\的所有匹配项

从字符串中删除\的所有匹配项
EN

Stack Overflow用户
提问于 2012-12-19 03:17:31
回答 6查看 81.7K关注 0票数 25

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

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

代码语言:javascript
复制
"[{\"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。因此,我需要将每次出现的\"替换为",如果\不是转义序列,这将是一项非常简单的任务。

我尝试使用以下代码。

代码语言:javascript
复制
String jsonFormattedString = jsonStr.replaceAll("\\", "");

但它给了我以下例外。

代码语言:javascript
复制
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):  ^

我的整个代码,以防它有任何用处:

代码语言:javascript
复制
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();
    }
}
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13939925

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档