兩種實現方式: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