I have:
String example = {"test":"true"}
but I want to have:
但我希望:
example = {"test":true}
How can I convert the first string to the second?
如何将第一个字符串转换为第二个字符串?
3 个解决方案
#1
0
Use regular expression and/or String class method like 'replaceAll'.
使用正则表达式和/或String类方法,如'replaceAll'。
#2
1
You can use String result = example.replaceAll(":\"true\"", ":true"};
and String result = example.replaceAll(":\"false\"", ":false"};
if there are only boolean values.
你可以使用String result = example.replaceAll(“:\”true \“”,“:true”};和String result = example.replaceAll(“:\”false \“”,“:false”};如果有的话只是布尔值。
#3
0
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)
如果你想要正确完成,那么你需要确保在json数据中处理其他条件。假设parse_data是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);
#1
0
Use regular expression and/or String class method like 'replaceAll'.
使用正则表达式和/或String类方法,如'replaceAll'。
#2
1
You can use String result = example.replaceAll(":\"true\"", ":true"};
and String result = example.replaceAll(":\"false\"", ":false"};
if there are only boolean values.
你可以使用String result = example.replaceAll(“:\”true \“”,“:true”};和String result = example.replaceAll(“:\”false \“”,“:false”};如果有的话只是布尔值。
#3
0
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)
如果你想要正确完成,那么你需要确保在json数据中处理其他条件。假设parse_data是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);