[英]Validate JSON against JSON schema (in Java)
我正在使用com.github.fge.jsonschema 。 在 Java 中工作。
以下是 JSON 架構。
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "Employee",
"description": "employee description",
"type": "object",
"properties": {
"eid": {
"description": "The unique identifier for a emp",
"type": "integer"
},
"ename": {
"description": "Name of the emp",
"type": "string"
},
"qual":{
"$ref": "#/definitions/qualification"
}
},
"definitions": {
"qualification":
{
"description": "Qualification",
"type": "string"
}
}
}
這是根據模式驗證的 JSON。
{
"eid":1000,
"ename": "mrun",
"qualification": "BE"
}
問題在於,如果我們傳遞任何錯誤的數據,它會正確驗證 eid 和 ename 的類型(即整數或字符串)。 例如:
{
"eid":"Mrun", //should be Integer
"ename": 72831287, //should be String
"qualification": 98372489 //should be String
}
如果我們僅通過錯誤類型進行限定,那么它驗證為真(即它不驗證限定類型可能是因為它是嵌套的)。
需要對整個 JSON 執行驗證。
是否有其他解決方案來驗證 JSON 中的嵌套對象?
提前致謝。
你的榜樣
{
"eid":"Mrun",
"ename": 72831287,
"qualification": 98372489
}
與您的架構不匹配。 您的架構需要像這樣的對象
{
"eid": "Mrun",
"ename": 72831287,
"qual": {
"qualification": 98372489
}
}
但是,如果您只想重用“限定”定義,您的架構應該類似於
"properties": {
"eid": {
"description": "The unique identifier for a emp",
"type": "integer"
},
"ename": {
"description": "Name of the emp",
"type": "string"
},
"qualification":{
"$ref": "#/definitions/qualification"
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.