簡體   English   中英

如何從JSON字符串中的布爾值中刪除引號?

[英]How do I remove quotes from a boolean value in a JSON string?

我有:

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

但我想擁有:

example = {"test":true}

如何將第一個字符串轉換為第二個字符串?

您可以使用String result = example.replaceAll(":\\"true\\"", ":true"};String result = example.replaceAll(":\\"false\\"", ":false"};如果存在只是布爾值。

如果您希望正確完成操作,則需要確保照顧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);

使用正則表達式和/或String類方法,例如'replaceAll'。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM