- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我在 JSONObject.toString(int) 中注意到了这一点文档
Throws: JSONException - If the object contains an invalid number.
如果事先进行了 NaN/Infinity 检查(在解析时和在 .put("blah", Double.NAN)
之类的语句中),对象怎么可能包含无效数字?哪些语句会抛出该异常?
我还在文档中读到 .toString()没有 args 不会抛出任何东西,所以我认为异常可能与作为参数传递的整数有关;但即使是零、负数或 Integer.MAX/MIN_VALUE,它仍然有效。
那么什么时候应该期望 .toString(int) 抛出异常以及应该如何处理呢?
最佳答案
toString()
方法不会抛出异常,因为它必须覆盖 java.lang.Object
的 public void toString()
方法的签名。在 org.json.JSONObject
中,通用 toString()
方法实际上失败了,因为源代码是:
/**
* Make a JSON text of this JSONObject. For compactness, no whitespace is
* added. If this would not result in a syntactically correct JSON text,
* then null will be returned instead.
* <p>
* Warning: This method assumes that the data structure is acyclical.
*
* @return a printable, displayable, portable, transmittable representation
* of the object, beginning with <code>{</code> <small>(left
* brace)</small> and ending with <code>}</code> <small>(right
* brace)</small>.
*/
public String toString() {
try {
return this.toString(0);
} catch (Exception e) {
return null;
}
}
这个方法依赖于toString(int)
方法,如果它抛出异常,它会捕获它并返回null。
根据toString(int)
的描述,当org.json.JSONObject
的其中一个元素中有一个无效数字时抛出异常;但是查看代码可能会因为其他原因抛出此异常。
当您使用 toString(int)
时,堆栈跟踪最终调用 write()
方法来解析对象本身,对于来自 json 的某些转换可能会抛出异常字符串对象:
static final Writer writeValue(Writer writer, Object value,
int indentFactor, int indent) throws JSONException, IOException {
if (value == null || value.equals(null)) {
writer.write("null");
} else if (value instanceof JSONObject) {
((JSONObject) value).write(writer, indentFactor, indent);
} else if (value instanceof JSONArray) {
((JSONArray) value).write(writer, indentFactor, indent);
} else if (value instanceof Map) {
new JSONObject((Map<String, Object>) value).write(writer, indentFactor, indent);
} else if (value instanceof Collection) {
new JSONArray((Collection<Object>) value).write(writer, indentFactor,
indent);
} else if (value.getClass().isArray()) {
new JSONArray(value).write(writer, indentFactor, indent);
} else if (value instanceof Number) {
writer.write(numberToString((Number) value));
} else if (value instanceof Boolean) {
writer.write(value.toString());
} else if (value instanceof JSONString) {
Object o;
try {
o = ((JSONString) value).toJSONString();
} catch (Exception e) {
throw new JSONException(e);
}
writer.write(o != null ? o.toString() : quote(value.toString()));
} else {
quote(value.toString(), writer);
}
return writer;
}
但是,如果正如您在问题(和@Carlos Rodriguez 的评论)中所说,所有检查都是在创建对象时执行的,那么 toString(int)
方法可能从未抛出异常。
希望对你有帮助
关于java - JSONObject.toString(int) 如何抛出 JSONException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32073494/
我有 7 个协议(protocol),我想在网站上显示。但该网站仅显示 4 个协议(protocol),尽管 JSON 文件中有 7 个协议(protocol)。我决定用调试器检查一下,它通过了 4
我在此代码中遇到 JSON 异常: JSONObject jObject = new JSONObject(JSONString); pontosUsuario.setIdUsuari
我创建了以下价格和移动型号名称的 json 数组。此数据是从表名称为“products”的 wamp phpmyadmin 服务器读取的: { "products": [
我正在从 linkedHashMap 获取 JSON 值 LinkedHashMap lst = JsonPath.parse(json).read("$.projects[0].issuetypes
我正在尝试使用来自 Guardian API 的 JSON 对象。 这是我调用的结果: https://pastebin.com/wqggLEeZ 这是我的代码 JSONObject root = n
我正在尝试从数据库中检索数据。我向此 PHP 文件发送请求: $code, "message"=>$message); echo json_encode($err); }
JSONObject login = new JSONObject(); login.put("Key1", "Value1"); 我只是想用键值对创建一个简单的 JSON 对象。我收到此异常“Unh
我试图解析下面的 json 并在第 150 行(在代码中标记)捕获了 JSONException,但我无法弄清楚为什么会发生。目标是找到特定包的测试列表并将其存储在 arraylist 中。 当我将
我在解析 JSON 时遇到很多问题,其中一个值具有空值。 { "available_from" : "2012-11-05T00:00:00Z", "available_to" : "2012-11-
我有一个带有一些 spring crud 存储库的经典 spring web 应用程序。 我正在尝试以经典的 Angular 形式保存我的实体,但我随机收到此错误: .w.s.m.s.DefaultH
我正在尝试将字符串转换为 JSONObject。这是我的代码: JSONObject obj = new JSONObject(str); Vehicle.feature 文件包含: Scen
@Override protected Integer doInBackground(Void... params) { String readMyJSON = rea
我想解析一些从网上下载的 JSONArray 中的 json。我认为这很简单,但无法让它发挥作用。 看来问题出在 JSON 格式上。我尝试了不同的方法修复它,但没有任何帮助。 我用这种方法下载字符串:
我收到以下错误: JSONException: No value for error 在 Android Studio 中的我的日志(查看日志截图的链接: /image/45eeF.png )上,它说
它可以很好地从数据库读取和写入,但会给出 JSONException 错误。任何帮助将不胜感激! 谢谢 最佳答案 此错误发生在 JSONObject 的 getter 函数中,这意味着您尝试使用 JS
我想从此链接获取json数据:link &这是我的代码: private static String url = "https://graph.facebook.com/fql?q=SELECT%20
描述:在 OnRespose 方法中,我可以在日志中看到从 froecast.io 接收的数据,但是当我传递“response.body().string()”时,即来自 Forecast.io 的数
我正在尝试从此网址中抓取书名:http://api.nytimes.com/svc/books/v2/lists/2010-10-01/trade-fiction-paperback?api-key=
我一直在尝试在 android studio 中为我的应用程序创建一个登录系统,但我不断收到此错误 org.json.JSONException: Value Connection of type j
我有一个 iOS 应用程序和一个 Android 应用程序,可以使用 RESTFul Web 服务。 当我向服务进行查询时,我得到一个像这样返回的 JSON 对象。 [ { "
我是一名优秀的程序员,十分优秀!