开发者

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);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜