如何从JSON字符串中的布尔值中删除引号?

时间:2011-05-11 13:48:58

标签: java json string

我有:

String example = {"test":"true"}

但我希望:

example = {"test":true}

如何将第一个字符串转换为第二个字符串?

3 个答案:

答案 0 :(得分:1)

如果只有布尔值,则可以使用String result = example.replaceAll(":\"true\"", ":true"};String result = example.replaceAll(":\"false\"", ":false"};

答案 1 :(得分:0)

使用regular expression和/或String类方法,例如'replaceAll'。

答案 2 :(得分:0)

如果你想要正确完成,那么你需要确保在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);
相关问题