我有方法必须检查JSON是否有效,可以在How to check whether a given string is valid JSON in Java找到,但它不起作用。
public static boolean isJson(String Json) {
Gson gson = new Gson();
try {
gson.fromJson(Json, Object.class);
return true;
} catch (com.google.gson.JsonSyntaxException ex) {
return false;
}
}
如果我将这个方法用于某个字符串,它总是返回true。例如:
System.out.println(renderHtml.isJson("{\"status\": \"UP\"}"));
它给了我true
和
System.out.println(renderHtml.isJson("bncjbhjfjhj"));
还给了我true
。
答案 0 :(得分:8)
虽然你可能很奇怪
"bncjbhjfjhj"
确实是有效的json,因为它是一个字符串,它是唯一的字符串。
根据不那么新的JSON RFC
JSON文本是序列化值。注意某些先前的 JSON的规范将JSON文本约束为对象或 阵列。仅生成对象或数组的实现 调用的JSON文本在所有方面都是可互操作的 实现将接受这些作为符合JSON文本。
答案 1 :(得分:6)
您不应使用Gson
进行此类验证:
Gson
是执行反序列化的对象,因此它将整个 JSON反序列化为内存中的对象。Gson
,我不知道,对某些无效的JSON可能不是很严格:bncjbhjfjhj
被反序列化为java.lang.String
个实例。惊喜,惊喜!private static final Gson gson = new Gson();
private static final String VALID_JSON = "{\"status\": \"UP\"}";
private static final String INVALID_JSON = "bncjbhjfjhj";
System.out.println(gson.fromJson(VALID_JSON, Object.class).getClass());
System.out.println(gson.fromJson(INVALID_JSON, Object.class).getClass());
输出:
com.google.gson.internal.LinkedTreeMap类 class java.lang.String
这里你可以做的是使用JsonReader
通过令牌读取传入的JSON令牌,从而使得给定的JSON文档在语法上是有效的。
private static boolean isJsonValid(final String json)
throws IOException {
return isJsonValid(new StringReader(json));
}
private static boolean isJsonValid(final Reader reader)
throws IOException {
return isJsonValid(new JsonReader(reader));
}
private static boolean isJsonValid(final JsonReader jsonReader)
throws IOException {
try {
JsonToken token;
loop:
while ( (token = jsonReader.peek()) != END_DOCUMENT && token != null ) {
switch ( token ) {
case BEGIN_ARRAY:
jsonReader.beginArray();
break;
case END_ARRAY:
jsonReader.endArray();
break;
case BEGIN_OBJECT:
jsonReader.beginObject();
break;
case END_OBJECT:
jsonReader.endObject();
break;
case NAME:
jsonReader.nextName();
break;
case STRING:
case NUMBER:
case BOOLEAN:
case NULL:
jsonReader.skipValue();
break;
case END_DOCUMENT:
break loop;
default:
throw new AssertionError(token);
}
}
return true;
} catch ( final MalformedJsonException ignored ) {
return false;
}
}
然后测试一下:
System.out.println(isJsonValid(VALID_JSON));
System.out.println(isJsonValid(INVALID_JSON));
输出:
真
假的
答案 2 :(得分:3)
根据How to check whether a given string is valid JSON in Java
,我找到了解决方案,但使用了org.json
库
public static boolean isJson(String Json) {
try {
new JSONObject(Json);
} catch (JSONException ex) {
try {
new JSONArray(Json);
} catch (JSONException ex1) {
return false;
}
}
return true;
}
现在,随机查看字符串bncjbhjfjhj
为false
,{"status": "UP"}
为真。
答案 3 :(得分:0)
另一种选择是将自定义的后检查应用到顶级json对象,如下所示:
SELECT
tb1.address,
tb1.user_name_hash
FROM my_table tb1
INNER JOIN my_table tb2
ON tb1.user_name_hash = tb2.user_name_hash
LEFT JOIN my_hash_table ht
ON tb1.user_name_hash = ht.user_name_hash AND tb1.id > tb2.id
WHERE ht.user_name_hash IS NULL;
答案 4 :(得分:0)
这对我有用
public static boolean isJson(String Json) {
Gson gson = new Gson();
try {
gson.fromJson(Json, Object.class);
Object jsonObjType = gson.fromJson(Json, Object.class).getClass();
if(jsonObjType.equals(String.class)){
return false;
}
return true;
} catch (com.google.gson.JsonSyntaxException ex) {
return false;
}
}