Java中可靠的JSON字符串验证器 [英] Reliable JSON string validator in Java

查看:103
本文介绍了Java中可靠的JSON字符串验证器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些可靠的JSON字符串验证器 - 一种吸收字符串并检查它是否是有效JSON的方法。示例:如果我传递 {color:red} {amount:15} 它将通过,但我的无效json之类的东西不会。简而言之,我需要一些像www.jsonlint.com验证器一样可靠的东西。顺便说一句 - 我对反序列化为java对象不感兴趣,因为这不是我的要求。我可能会收到一个任意字符串,所有我要做的就是验证它是否有一个有效的JSON格式。

I need some help coming with a reliable JSON string validator - a method which intake a string and checks if it's a valid JSON. Example: if I pass {"color":"red"} or {"amount":15} it will pass but something like "My invalid json" will not. In short I need something that is as reliable as www.jsonlint.com validator. BTW - I'm not interested in deserializing into java object, because that's not my requirement. I may receive an arbitrary string and all I have to do is validate it has a valid JSON format.

我已经在这个论坛上研究了几个关于java主题的帖子JSON字符串验证。

I have already researched on this forum several posts on the subject regarding java JSON string validations.

到目前为止我做了什么:

What I have done so far:

我尝试使用这些类: org.json.JSONObject org.json.JSONArray 按以下方式:

I tried using these classes: org.json.JSONObject and org.json.JSONArray in the following manner:

private static boolean isValidJSONStringObject(String requestBody){
    try {
        new JSONObject(requestBody);
    } catch (JSONException jsonEx) {
        return false;
    }
    return true;
}

private static boolean isValidJSONStringArray(String requestBody) {
    try {
        new JSONArray(requestBody);
    } catch (JSONException jsonEx) {
        return false;
    }
    return true;
}

但是,以下字符串(整行)仍然存在,并且它们不应该't:

However, the following strings (entire lines) still go through, and they shouldn't:

{"color":"red"}{"var":"value"}

[1,2,3][true,false]

换句话说,当我有对象/数组重复w / out某些父对象中的任何封装。如果你在www.jsonlint.com验证器中粘贴这些行,它们都会失败。

in other words when I have objects/arrays repeated w/out any encapsulation in some parent object. If you paste these lines in www.jsonlint.com validator they both fail.

我知道总有一个正则表达式选项,但是我无法保证100%因为JSON和那些正则表达式的递归性质将会相当复杂。

I know there is always a regex option but I gess that cannot be guaranteed 100% because of the recursive nature of JSON and those regex expressions are going to be rather complex.

任何帮助将不胜感激!

推荐答案

Gson可以解决这个问题。下面是一个例子:

Gson can handle this. Here is an example:

public boolean isValid(String json) {
    try {
        new JsonParser().parse(json);
        return true;
    } catch (JsonSyntaxException jse) {
        return false;
    }
}

String json = "{\"color\":\"red\"}{\"var\":\"value\"}";
System.out.println(isValid(json));

请注意,Gson确实允许输入JSON的一些宽大处理,这可能是不可取的。例如,解析器会自动将未加引号的键转换为引用的键。根据您的预期用途,这可能是也可能不是交易破坏者。

Note that Gson does allow some leniency in input JSON, which might not be desired. For example, unquoted keys are automatically converted to quoted ones by the parser. This may or may not be a deal-breaker depending on your expected usage.

这篇关于Java中可靠的JSON字符串验证器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆