从字符串中删除所有出现的\ [英] Remove all occurrences of \ from string
问题描述
服务器向我发送以下字符串。
{{\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\\ \\ :空,\ DealVotes\ :[],\ DealId\ :2,\ CompanyId\ :1,\ StartDate\ :\ 2012-12 -16T00:00:00\,\EndDate\:\2012-12-17T00:00:00\,\\CouponCode \:\Test coupon 2 \ ,\Description\:\测试描述2 \,\\VoteUp\:null,\投票Down\: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 \:\测试代码3 \,\说明\\ :\Test Description 3\,\VoteUp\:null,\VoteDown\:null,\ViewCount\:null,\Title\ \Deal 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\ :空,\ ViewCount\ :空,\ Title\ :\ T最新交易4 \},{\DealComment\:null,\DealVotes \:[],\DealId\:5,\CompanyId\ \ StartDate\:\ 2012-12-12T00:00:00\,\ EndDate\:\ 2012-12-14T00:00:00\ \ CouponCode\:\AwD\,\描述\:\非常好的处理!\,\VoteUp\:null,\VoteDown\ :null,\ViewCount\:null,\Title\:\Awesome Deal 1 \}]
现在,如果仔细查看字符串,您会注意到它包含一个 \
而不是每个 。该字符串现在无法格式化为JSONArray。所以,我需要用
替换每一次
\
,这将是一个非常简单的任务, \
不是转义序列。 / p>
我尝试使用以下代码。
String jsonFormattedString = jsonStr。全部替换(\\, );
但它给了我以下例外。
12-19 00:35:59.575:W / System.err(444):java.util.regex.PatternSyntaxException:语法错误U_REGEX_BAD_ESCAPE_SEQUENCE靠近索引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); //超时限制
HttpResponse响应;
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(\\,); //给出错误
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(异常e)
{
e.printStackTrace();
}
}
尝试这样:
String jsonFormattedString = jsonStr.replaceAll(\\ \\,);由于反斜杠是正则表达式中的转义字符( replaceAll()),因此,
< 接收一个作为参数),它也必须被转义。
I am trying to get an array of objects from the server, using JSON.
The server sends me the following string.
"[{\"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\"}]"
Now, if you look at the string closely, you would notice that it contains an \"
instead of every "
. The string cannot be formatted to a JSONArray right now. So, I need to replace every occurrence of \"
with "
, which would have a pretty easy task, had \
not been an escape sequence.
I tried using the following code.
String jsonFormattedString = jsonStr.replaceAll("\\", "");
But it gives me the following exception.
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): ^
My whole code, in case it is of any use :
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();
}
}
Try this:
String jsonFormattedString = jsonStr.replaceAll("\\\\", "");
Because the backslash is the escaping character in a regular expression (replaceAll()
receives one as parameter), it has to be escaped, too.
这篇关于从字符串中删除所有出现的\的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!