如何使用模式C#验证Json



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

string data = File.ReadAllText(@"C:UsersAman.SharmaSourceReposValidatorValidatortestData.json");
string schema = File.ReadAllText(@"C:UsersAman.SharmaSourceReposValidatorValidatorV2JsonSchema.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"
}
}
}

我在使用这种方法时也有类似的错误。

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

如果有效,请用忽略\r\n和n的不那么粗鲁的方法替换。

相关内容

  • 没有找到相关文章

最新更新