Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python json模式中的引用有问题_Python_Json_Jsonschema - Fatal编程技术网

Python json模式中的引用有问题

Python json模式中的引用有问题,python,json,jsonschema,Python,Json,Jsonschema,我在验证一个JSON模式时遇到了问题,它引用了另一个模式和Python。我有两个模式(我可以创建的最简单的模式): parent.json { "$ref": "REFERENCE_TO_CHILD_JSON" } child.json { "type": "number" } 因此,如果我理解正确,JSON{5}将被该验证器接受,而JSON{'hello'}不会。我将这两个JSON、上面的两个JSON模式和下面的代码放在同一个文件夹中: python_code.py from js

我在验证一个JSON模式时遇到了问题,它引用了另一个模式和Python。我有两个模式(我可以创建的最简单的模式):

parent.json

{
  "$ref": "REFERENCE_TO_CHILD_JSON"
}
child.json

{
  "type": "number"
}
因此,如果我理解正确,JSON
{5}
将被该验证器接受,而JSON
{'hello'}
不会。我将这两个JSON、上面的两个JSON模式和下面的代码放在同一个文件夹中:

python_code.py

from jsonschema import validate
import json

with open('parent.json', 'r') as f:
   my_schema = json.load(f)

with open('json_A.json', 'r') as f:
   my_json = json.load(f)

validate(my_json, my_schema)
现在,我的问题是:我必须在父Json模式中插入什么作为引用?

  • 我尝试使用子架构的名称(因此,仅
    child.json
    ),但它给出了一个错误:
    jsonschema.exceptions.refresolutioneror:unknown url type
  • 我尝试了
    文件:child.json
    ,它给出了一个错误:
    jsonschema.exceptions.refresolutioneror:urlopen error[WinError 2]找不到文件

我做错什么了吗?

无论是
{5}
还是
{'hello'}
都不是有效的JSON。您使用的是什么库?JSON模式对“文件系统中的文件”一无所知。通常,您在模式中定义模式的
$id
,然后加载它们(正如您所做的那样),并通过提供的
$id
引用它们。我应该在$id字段中输入什么?一旦我把文件夹移到另一个地方,我应该改变那个值吗?