简体   繁体   中英

Need to escape the double or some special characters in a String

I want to convert a push message from server with a string with special characters.

I use JSONObject to convert it before using the text.

This is my message from server :

{"aps":{"alert":{"body":**"push message 4 test Close Notification in \\"Android\\" PU45"**,"action-loc-key":"OK","screenId":"110","sdata":"sid=SER020","launch-image":"appicon"},"sound":"ring1"}} .

Highlighted in bold is my string. I want to escape the double quotes in the string, since the substring Android should be shown as text with double while showing in the popup.

Can anyhelp on this issue?

Thanks all in advance, Janardhan.

String jsonString = "YOUR JSON HERE";
JsonObject json = new JsonObject(jsonString);
JsonObject aps = json.getJsonObject("aps");
JsonObject alert = aps.getJsonObject("alert");
String body = alert.getString("body");

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

Why don't you just remove the quotes while you send from server itself. Check this out

for example: For adding Quote before and after that android pattern, do like this.

Pattern p = Pattern.compile("\"([^\"]*)\"");
Matcher m = p.matcher(line);
while (m.find()) {
  String fetched = "\"+m.group+"\"";
  System.out.println(fetched);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM