I am trying to replace all the square brackets with the curly brackets.
Example:
String string = "{\"test\":{\"id\":["4,5,6"}]},{\"Tech\":["Java,C++"]}}";
I want to remove square brackets([,]) and replace them with curly brackets({,}).
Like:
"{\"test\":{\"id\":{"4,5,6"}}},{\"Tech\":{"Java,C++"}}}";
I have tried:
string = string.replace("\\[", "\\{").replace("\\]", "\\}");
But didnt worked.Need some suggestions here.
You don't need \\\\[
and \\\\{
for that you don't get the correct result, instead you can use :
System.out.println(string.replace("[", "{").replace("]", "}"));
Note
Your are using an invalid String you have to use \\
before "\\""
like this :
String string = "{\"test\":{\"id\":[\"4,5,6\"}]},{\"Tech\":[\"Java,C++\"]}}";
you can either do this:
str.replace('[', '{').replace(']', '}');
or you can do this:
str.replaceAll("\\[", "{").replaceAll("\\]", "}");
Replace uses Characters, thus the ' ' whereas replaceAll uses Regex. This is why you should escape your bracket there.
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.