gpt4 book ai didi

php - 针对 JSON 模式的 JSON 验证

转载 作者:搜寻专家 更新时间:2023-10-31 21:37:55 25 4
gpt4 key购买 nike

我正在尝试根据我给定的模式验证 JSON 对象。

JSON数据如下:

{
"list": {
"places": [
{
"name": "Loopsiloo",
"foursquareID": "54a6s5D4a6s5d4a6s5D4",
"lat": 26.6546845354889,
"lon": -99.6846873700158
},
{
"name": "Loopsiloo",
"foursquareID": "54a6s5D4a6s5d4a6s5D4",
}
],
"title": "Foo Bar",
"dateCreated": "2013-01-29T14: 19: 30Z"
}

JSON Schema 如下:

{
"type":"object",
"$schema": "http://json-schema.org/draft-03/schema",
"required":true,
"properties":{
"list": {
"type":"object",
"id": "list",
"required":true,
"properties":{
"dateCreated": {
"type":"string",
"id": "dateCreated",
"required":true
},
"places": {
"type":"array",
"minitems": "1",
"id": "places",
"required":true,
"items":
{
"type":"object",
"required":true,
"properties":{
"note": {
"type":"string",
"id": "note",
"required":false
},
"foursquareID": {
"type":"string",
"id": "foursquareID",
"required":true
},
"lat": {
"type":"number",
"id": "lat",
"required":true
},
"lon": {
"type":"number",
"id": "lon",
"required":true
},
"name": {
"type":"string",
"id": "name",
"required":true
}
}
}


},
"title": {
"type":"string",
"id": "title",
"required":true
}
}
}
}

我正在使用 PHP 中的 JsonSchema\Validator 验证此 JSON。

$validator = new JsonSchema\Validator;
$validator->check($data, file_get_contents(__DIR__ . '/../model/api-schema.json'));

我的问题是验证器每次都验证 JSON 对象是否正确。在顶部的示例中,缺少属性“lat”和“lon”。即使我省略了整个“地点”、“标题”或“创建日期”属性,它也会被验证为正确。

有什么我想念的吗?我浏览了 JSON 模式的文档,但没有任何帮助。

最佳答案

这对我有用。

$validator = new JsonSchema\Validator;
$schema = file_get_contents(__DIR__ . '/../model/api-schema.json');
$validator->check(json_decode($data), json_decode($schema));

关于php - 针对 JSON 模式的 JSON 验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14855984/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com