【发布时间】:2015-10-08 04:35:52
【问题描述】:
我必须检查给定的值是否为 JSONObject.... 示例输入:
Object obj = "{} testing"
我正在检查以下代码:
public boolean isJSONValid(Object obj) {
try {
new JSONObject(obj);
} catch(JSONException e) {
return false;
}
return true;
}
但是对于上面的输入,它是真实的,我正在使用 org.json jar 文件。
【问题讨论】:
-
我觉得这个问题和这个问题很相似:How to check if a string is a valid JSON string in JavaScript without using Try/Catch。您可以使用建议的正则表达式或
JSON.parse(str);替代 try/catch -
谢谢,我会尝试使用正则表达式。