How do I remove quotes from a boolean value in a JSON string?
I have:
String example = {"test":"true"}
but I want to have:
开发者_如何学编程example = {"test":true}
How can I convert the first string to the second?
You can use String result = example.replaceAll(":\"true\"", ":true"};
and String result = example.replaceAll(":\"false\"", ":false"};
if there are only boolean values.
Use regular expression and/or String class method like 'replaceAll'.
If you want it done right, then you need to make sure to take care of other conditions in the json data. Assuming parse_data is JSONObject (java)
String raw_tag = parse_data.toString();
raw_tag = raw_tag.replaceAll(":\"true\"", ":true");
raw_tag = raw_tag.replaceAll(",\"true\"", ",true");
raw_tag = raw_tag.replaceAll("\\[\"true\"", "\\[true");
raw_tag = raw_tag.replaceAll(":\"false\"", ":false");
raw_tag = raw_tag.replaceAll(",\"false\"", ",false");
raw_tag = raw_tag.replaceAll("\\[\"false\"", "\\[false");
System.out.print(parseData);
精彩评论