0

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.

Kamlesh Arya
  • 4,864
  • 3
  • 21
  • 28
Janardhan
  • 19
  • 4

2 Answers2

1
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("\"", "");
Manitoba
  • 8,522
  • 11
  • 60
  • 122
0

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);
}
Community
  • 1
  • 1
Vikas B L
  • 397
  • 1
  • 10
  • But i need that string "Android" should be in double quotes while displaying to the user in a popup. This is my actual data 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"}} – Janardhan Dec 10 '13 at 10:47
  • This is my bundle data in android: onMessage() Received message intent = Bundle[{from=319471187667, mxkey={\"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\"}}, collapse_key=do_not_collapse}] – Janardhan Dec 10 '13 at 10:51
  • Check the link I have posted and add " before and after that value you retrieved manually. – Vikas B L Dec 10 '13 at 10:55
  • @Janardhan: Please refer to the code I added. Change accordingly and use it. – Vikas B L Dec 10 '13 at 11:00
  • @Janardhan: If that code worked for you please mark it as answer so that other can make use of it. – Vikas B L Dec 12 '13 at 07:42