tsync
tsync

Reputation: 2417

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?

Upvotes: 2

Views: 5025

Answers (3)

user2782868
user2782868

Reputation: 1

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

Upvotes: 0

Aito
Aito

Reputation: 6872

Use regular expression and/or String class method like 'replaceAll'.

Upvotes: 0

MByD
MByD

Reputation: 137382

You can use String result = example.replaceAll(":\"true\"", ":true"}; and String result = example.replaceAll(":\"false\"", ":false"}; if there are only boolean values.

Upvotes: 1

Related Questions