python中的json参考提取 [英] json reference extraction in python

查看:60
本文介绍了python中的json参考提取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Json不仅可用作API的通信工具,而且还可以用作将正在运行的程序配置为初始化的标记.

Json is not only useful as a communication tool for APIs, but also may be used as a markup for configuring running programs as initialization.

我在json模式中遇到了引用的用途重用.

I encountered the use of references in json schema for the purpose of reuse.

由于json模式是有效的json,所以我期望python json库能够扩展引用.

Since json schema is valid json, I had expected the python json library to have the ability to expand references.

$ cat test.json
{ 
  "template":{
    "a":"a",
    "b":"b",
    "pi":3.14
  },
  "value": { "$ref":"#/template"}
}
python -c "from json import load; fp = open(\"test.json\",\"r\"); print(load(fp))"
{'template': {'a': 'a', 'b': 'b', 'pi': 3.14}, 'value': {'$ref': '#/template'}}

在python中扩展引用的最简单方法是什么,因为python字典无法指向自身的其他部分(我认为)?

What is the simplest way to expand the references in python, since python dicts cannot point to other parts of themselves (I think)?

推荐答案

json 库不支持引用,但是 jsonref 支持.

The json library does not support references, but jsonref does.

jsonref是一个库,用于自动取消引用Python(支持Python 2.6+和Python 3.3+)的JSON参考对象.

jsonref is a library for automatic dereferencing of JSON Reference objects for Python (supporting Python 2.6+ and Python 3.3+).

从文档中

from pprint import pprint
import jsonref

# An example json document
json_str = """{"real": [1, 2, 3, 4], "ref": {"$ref": "#/real"}}"""
data = jsonref.loads(json_str)
pprint(data)  # Reference is not evaluated until here
{'real': [1, 2, 3, 4], 'ref': [1, 2, 3, 4]}

这篇关于python中的json参考提取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆