gpt4 book ai didi

java - 字符串解释为 boolean 值

转载 作者:行者123 更新时间:2023-12-01 18:36:38 25 4
gpt4 key购买 nike

这是我的代码:

try {
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
String temp = reader.readLine();
if(temp!=null)
result = new JSONObject(temp);
} catch (Exception e) {
e.printStackTrace();
}

如您所见,我将 String 分配给 String 类型对象,但是当代码运行时,result="true" 并且出现异常:

Value true of type java.lang.Boolean cannot be converted to JSONObject

这令人沮丧..我应该对 String 对象进行强制转换还是使用 .toString() 吗?为什么它会自动转换为 boolean 值?

更新我发现这不是 Java 问题,而是 JSONObject 构造函数未接收有效的 JSONObject。

最佳答案

您所观察到的情况是预期的。引用自JSONObject :

Strings do not need to be quoted at all if they do not begin with a quote or single quote, and if they do not contain leading or trailing spaces, and if they do not contain any of these characters: { } [ ] /
\ : , = ; #
and if they do not look like numbers and if they are not the reserved words true, false, or null.

关于java - 字符串解释为 boolean 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21695490/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com