从 JSON.parse 捕获异常的正确方法
捕获无效 JSON 解析错误的最有效方法是将 JSON.parse() 调用放入 try/catch 块中。
示例
function parseJSONSafely(str) {
try {
return JSON.parse(str);
}
catch (e) {
console.err(e);
// Return a default object, or null based on use case.
return {}
}
}
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP