更新时间:2022-06-01 07:13:35
一个疯狂的想法,尝试解析它并捕获异常:
A wild idea, try parsing it and catch the exception:
import org.json.*;
public boolean isJSONValid(String test) {
try {
new JSONObject(test);
} catch (JSONException ex) {
// edited, to include @Arthur's comment
// e.g. in case JSONArray is valid as well...
try {
new JSONArray(test);
} catch (JSONException ex1) {
return false;
}
}
return true;
}
此代码使用可用的 org.json JSON API 实现在 github 上,在 maven 中 部分在 Android 上.
This code uses org.json JSON API implementation that is available on github, in maven and partially on Android.