2013-12-10 138 views
0

我想将带有特殊字符的字符串从服务器转发推送消息。我使用JSONObject在使用文本之前将其转换。需要转义字符串中的双字符或某些特殊字符

这是从服务器我的信息:

{"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"}}

以粗体突出显示的是我的字符串。我想逃避字符串中的双引号,因为子字符串Android应该在弹出窗口中显示为带有双精度的文本。

可以帮助解决这个问题?

非常感谢, Janardhan。

回答

0

为什么不从服务器本身发送引号?检查this

例如:为了在android模式之前和之后添加引用,请这样做。

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

但我需要一个字符串“Android”的应该是在双引号同时显示在一个弹出窗口中的用户。这是我的服务器实际数据: {“aps”:{“alert”:{“body”:推送消息4测试关闭通知在“Android”PU45,“action-loc-key”:“OK”,“screenId “:”110“,”sdata“:”sid = SER020“,”launch-image“:”appicon“},”sound“:”ring1“}} – Janardhan

+0

这是我在android中的包数据: onMessage消息intent = Bundle [{from = 319471187667,mxkey = {\“aps \”:{\“alert \”:{\“body \”:push message 4 test关闭通知在\“Android \”PU45,\“action -loc密钥\ “:\” OK \ “\ ”screenId \“:\ ”110 \“ \ ”SDATA \“:\ ”SID = SER020 \“ \ ”启动图像\“:\” APPICON \“},\”声音\“:\”ring1 \“}},collapse_key = do_not_collapse}] – Janardhan

+0

检查我发布的链接并添加”之前和之后手动检索的值 – user1728071

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("\"", ""); 
+0

非常感谢。让我尝试。 – Janardhan

+0

这种方法并没有为我工作。 – Janardhan