方法1:先給一個比較暴力的方法
try {JSONObject o = new JSONObject(yourString);
} catch (JSONException e) {LOGGER.error("No valid json");
}
方法2:
Object json = new cn.hutool.json.JSONTokener("[{\"name\":\"test\"}]").nextValue();
if (json instanceof cn.hutool.json.JSONObject || json instanceof cn.hutool.json.JSONArray) {System.out.println(true);
} else {System.out.println(false);
}
參考:Mybatis---TypeHandler,輕松應對Mysql的JSON類型本文主要介紹了 MyBatis 中 Type - 掘金?
fastjson JSONValidator 的使用記錄,判斷一個字符串是否是正確的json格式 - loveCrane - 博客園?