【问题标题】:How to validate Json With schema C#如何使用模式 C# 验证 Json
【发布时间】:2023-02-01 00:23:10
【问题描述】:

我正在尝试使用 JSON 模式验证 JSON(API 响应)

string data = File.ReadAllText(@"C:\Users\Aman.Sharma\Source\Repos\Validator\Validator\testData.json");
string schema = File.ReadAllText(@"C:\Users\Aman.Sharma\Source\Repos\Validator\Validator\V2JsonSchema.json");

var model = JObject.Parse(data);
var json_schema = JSchema.Parse(schema);

bool valid = model.IsValid(json_schema, out IList<string> messages);

但是问题只是在第一次出现错误时抛出错误并跳过另一部分。 有没有办法将整个 JSON 与模式进行比较并为每条记录抛出错误? 此外,如果我更改架构,此方法始终会传递 JSON。

样本数据

{
  "id": "e1110047-b606-4fb3-84c6-28f7d5456e11",
  "accountInactiveDate": "0001-01-01T00:00:00Z",
  "accountOpenDate": "0001-01-01T00:00:00Z",
  "accountTypeIds": [ 4000 ],
  "address": {
    "city": "vVjEKwUP",
    "addressTypeId": 1000,
    "countryISOCode": "GBR",
    "street1": "xTMksdLL",
    "zipCode": "12345"
  },
  "annualRevenue": 0.0,
  "email": {
    "emailTypeId": 1000,
    "email": "zvvzwsdv@yopmail.com"
  },
  "homeLocationNumber": "316",
  "industryTypeId": 0,
  "isDeleted": false,
  "name": "AccounttxecJizQ",
  "parentAccountId": "00000000-0000-0000-0000-000000000000",
  "phone": {
    "phoneTypeId": 1000,
    "number": "+44 123456"
  },
  "productsTypeIds": [ 3000 ],
  "timeStamp": "\"2e001932-0000-0d00-0000-63315d8e0000\"",
  "links": [
    {
      "href": "https://api-test.QA.cloud/companies/api/v2/accounts/e1110047-b606-4fb3-84c6-28f7d5456e11",
      "rel": "Self"
    }
  ],
  "isBlocked": false,
  "isComplete": true,
  "createDate": "2022-09-26T08:06:38.2263447Z",
  "systemCaller": "qa-user2-automationtests",
  "originSystemCaller": "qa-user2-automationtests",
  "originCreateDate": "2022-09-26T08:06:38.2263447Z"
}

图式

{
  "type": "object",
  "properties": {
    "searchMode": {
      "enum": [
        "Any",
        "All"
      ],
      "type": "string"
    },
    "queryType": {
      "enum": [
        "Simple",
        "Full"
      ],
      "type": "string"
    },
    "select": {
      "type": "array",
      "items": {
        "type": "string"
      }
    },
    "searchFields": {
      "type": "array",
      "items": {
        "type": "string"
      }
    },
    "orderBy": {
      "type": "array",
      "items": {
        "type": "string"
      }
    },
    "highlightPreTag": {
      "type": "string"
    },
    "highlightPostTag": {
      "type": "string"
    },
    "highlightFields": {
      "type": "array",
      "items": {
        "type": "string"
      }
    },
    "filter": {
      "type": "string"
    },
    "facets": {
      "type": "array",
      "items": {
        "type": "string"
      }
    },
    "scoringProfile": {
      "type": "string"
    },
    "scoringParameters": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "name": {
            "type": "string"
          },
          "values": {
            "type": "array",
            "items": {
              "type": "string"
            }
          }
        }
      }
    },
    "page": {
      "format": "int32",
      "type": "integer"
    },
    "pageSize": {
      "format": "int32",
      "type": "integer"
    },
    "includeTotalResultCount": {
      "type": "boolean"
    }
  }
}

【问题讨论】:

标签: c# json json.net jsonschema


【解决方案1】:

我用这种方法有类似的错误。

尝试在从文件读取数据的代码末尾添加.Replace(" ", "").Replace(" ", "")

如果它有效 - 用不那么粗鲁的忽略方法代替 和 .

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-26
    • 1970-01-01
    • 1970-01-01
    • 2019-12-14
    • 2022-01-02
    • 1970-01-01
    相关资源
    最近更新 更多