两种实现方式:java
public static boolean isJSON2(String str) { boolean result = false; try { Object obj=JSON.parse(str); result = true; } catch (Exception e) { result=false; } return result; }
代码实现以下:json
public static boolean getJSONType(String str) { boolean result = false; if (StringUtils.isNotBlank(str)) { str = str.trim(); if (str.startsWith("{") && str.endsWith("}")) { result = true; } else if (str.startsWith("[") && str.endsWith("]")) { result = true; } } return result; }
第一种方式比较校验比较严格,校验也比较准确。第二种判断比较简单,适合用于约定数据。spa