字符串替换不起作用
String jsonresponseString ="�� ÿ0{"sid":"QtRm47A_2COfPAO-AANH","upgrades":["websocket"],"pingInterval":25000,"pingTimeout":60000}"
我试过这段代码
String responseString1 = jsonresponseString.replace("�� ÿ0", "");
我想要这样的输出
{"sid":"v3h9kTn3woDQEijKAANS","upgrades":["websocket"],"pingInterval":25000,"pingTimeout":60000}
答案 0 :(得分:2)
第一行
String responseString = �� ÿ0{"sid":"v3h9kTn3woDQEijKAANS","upgrades":["websocket"],"pingInterval":25000,"pingTimeout":60000}
不是有效的java代码。你有没有从某个程序的输出中复制粘贴它?它应该像
String responseString = "{\"sid\":\"v3h9kTn3woDQEijKAANS\", ...}"
我建议你仔细检查生成responseString
的内容。
答案 1 :(得分:1)
试试这段代码:
String printjson(String str) {
// \{\s*\".*\}$|^\[\n?\{\s*\".*\}\n?\]$~s
Pattern rulePattern = Pattern.compile("\\{\\s*\\\".*\\}$|^\\[\\n?\\{\\s*\\\".*\\}\\n?\\]$~s");
Matcher a = rulePattern.matcher(str);
if (a.find()) {
str = a.group();
}
return str;
}
System.out.println(printJson("�� ÿ0{"sid":"QtRm47A_2COfPAO-AANH","upgrades":["websocket"],"pingInterval":25000,"pingTimeout":60000}")