我正在尝试用大括号替换所有方括号。
示例:
String string = "{\"test\":{\"id\":["4,5,6"}]},{\"Tech\":["Java,C++"]}}";
我想删除方括号([,])并将其替换为大括号({,})。
喜欢:
"{\"test\":{\"id\":{"4,5,6"}}},{\"Tech\":{"Java,C++"}}}";
我已经尝试过:
string = string.replace("\\[", "\\{").replace("\\]", "\\}");
但没有成功。这里需要一些建议。
最佳答案
您不需要 \\[
和 \\{
因为您不会得到正确的结果,而是可以使用:
System.out.println(string.replace("[", "{").replace("]", "}"));
注意
您使用的字符串无效,您必须在 "\""
之前使用 \
,如下所示:
String string = "{\"test\":{\"id\":[\"4,5,6\"}]},{\"Tech\":[\"Java,C++\"]}}";
关于java - 删除方括号并替换为大括号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42691973/