1、简朴推断是不是为json花样 ,推断划定规矩:推断首尾字母是不是为{}或[],假如都不是则不是一个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; }
2、经由过程fastjson剖析来推断,剖析胜利,是json花样;不然,不是json花样
代码完成以下:
public static boolean isJSON2(String str) { boolean result = false; try { Object obj=JSON.parse(str); result = true; } catch (Exception e) { result=false; } return result; }
引荐教程:java入门教程
以上就是java中怎样推断字符串是不是为json花样的细致内容,更多请关注ki4网别的相干文章!