【问题标题】:Validating jsonSchema against a Json String针对 Json 字符串验证 jsonSchema
【发布时间】:2018-04-06 17:11:44
【问题描述】:

我有一个输入 Json,我想在 JAVA Rest API 中针对服务器端的模式进行验证。我想在模式中调用自定义函数。要遵循的标准是什么,是否有任何样品。在编写模式时也有一个标准,杰克逊是否提供任何模式解析器。

我有一个提供给我的示例架构,我想在危险属性中调用自定义函数

    {
  "$id": "http://example.com/example.json",
  "type": "object",
  "definitions": {

  },
  "$schema": "http://json-schema.org/draft-07/schema#",
  "properties": {
    "General": {
      "$id": "/properties/General",
      "type": "object",
      "properties": {
        "Name": {
          "$id": "/properties/General/properties/Name",
          "type": "string",
          "title": "The Name Schema ",
          "default": ""
        },
        "Peril": {
          "$id": "/properties/General/properties/Peril",
          "type": "string",
          "title": "The Peril Schema ",
          "default": ""
        }
      }
    }
  }

我有以下 json。我想在危险值上调用自定义函数

    {
  "General": {
    "Name": "NAEQ_DCN_MAR21",    
    "Peril": "Earthquake"
  }
}

【问题讨论】:

    标签: json jsonschema jsonparser


    【解决方案1】:

    您的 JSON 架构正在使用规范的 Draft 7,据我所知,Jackson 不支持该规范。要支持 Draft 7,您可以使用Everit's JSON schema validator。您可以在自述文件中找到 JSON 验证/解析示例。它使用org.json API,因此该API 的JSONObject 类将允许您访问Peril 值。

    【讨论】:

    • 我给出的例子是一个简单的 json,但我有多个需要运行的函数和不同的字段。我该如何实现。
    • 同样的事情。 API 的 JSONObject 允许您使用 get*() 方法之一获取 JSON 中的任何内容:stleary.github.io/JSON-java/org/json/JSONObject.html
    猜你喜欢
    • 1970-01-01
    • 2013-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-19
    • 1970-01-01
    • 2018-11-18
    • 1970-01-01
    相关资源
    最近更新 更多