如何在java中验证JSON对象? [英] How to validate a JSON object in java?

查看:993
本文介绍了如何在java中验证JSON对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用sf.json库在我的Web应用程序中映射传入请求的表单数据。

I use sf.json library to map form data for incoming request in my web application in java.

让我们说传入的请求是 http:// localhost:8080 / app / addProfile ,表单数据为:

Lets say the incoming request is http://localhost:8080/app/addProfile with form data as:

formData: {  
   "name":"applicant Name",
   "Age":"26",
   "academics":{  
      "college":"80",
      "inter":"67",
      "matriculation":"89"
   },
   "skill":{  
      "computer":"c,c++,java",
      "maths":"limit,permutation,statistics"
   },
   "dateOfBirth":"09-07-1988"
}

服务器端:

String requestFormData=request.getParameter("formData");
JSONObject formData = JSONObject.fromObject(requestFormData);
String name= formData.getString("name");

if(name.length>70){
//error message for length validation
}

if(!name.matches("regex for name"){
//error message for name validation
}
...
...
...

这种方法的主要问题是如果在 JSON 结构中进行了少量修改,然后需要修改整个代码。

The main problem with this approach is if there is minor modification in the JSON structure, then the entire code needs to be modified.

是否有任何api我可以配置验证所需的规则?

Is there is any api where i can configure the rules which are required for validation?

推荐答案

您可以使用Json验证器: -
https://github.com/fge/json-schema-validator

You can use the Json validator: - https://github.com/fge/json-schema-validator

或者您可以尝试使用解析Json Google Gson并捕获语法异常以验证它如下所示: -

Or you can simply try to parse the Json using Google Gson and catch syntax exception to validate it like below :-

try{
JsonParser parser = new JsonParser();
parser.parse(passed_json_string);
} 
catch(JsonSyntaxException jse){
System.out.println("Not a valid Json String:"+jse.getMessage());
}

对于通用数据验证,在Json模式中定义规则,然后只验证传入Json对这个模式。

在模式中你可以定义它可以包含的值的类型,范围等。

对于模式生成,你可以使用在线工具,如: - < a href =http://jsonschema.net/#/ =noreferrer> http://jsonschema.net/#/

For generic data validation, define the rules in your Json schema and then just validate the incoming Json against this schema.
In schema you can define the type of values it can contain, range etc.
For schema generation, you can use online tool like :- http://jsonschema.net/#/

您可以参考这篇文章,快速了解json架构: - http://json-schema.org/ example1.html

You can refer this post, to have quick understanding of json schema:- http://json-schema.org/example1.html

示例: -

"price": {
            "type": "number",
            "minimum": 0,
            "exclusiveMinimum": true
        }

上面的代码定义了Json模式中的价格,当Json对象针对此模式进行验证时,它将确保价格不应为零,它应该是大于零,它应该是一个数。如果价格中传递了字符串或零或一些负值,则验证将失败。

Above code defines the price in Json schema, when Json object is validated against this schema, it will ensure that price shouldn't be zero, it should be more than zero and it should be a number. If a string or zero or some negative value is passed in price, the validation will fail.

这篇关于如何在java中验证JSON对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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