如何校验一个字符串是否是可以正确序列化的JSON字符串呢?

方法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 - 博客园 

你可能感兴趣的:(java,json)