Python如何将OpenAPI JSON引用替换为实际值,即$ ref ='#/hey/wtf/〜1Something〜1Nothing/yeah yeah yeah yeah'
我已经习惯了XPATH和DOM在PHP上 使用这个,但接缝了klunky p是从json.loads(文件)尝试的JSON对象
string="#/paths/~1golly~1gee/get/parameters/0"
def string_todictreference(string, p=None):
token = string.lstrip('#/').split('/')
count = 0
for i in token:
token[count] = i
if '~1' in i:
token[count] = i.replace('~1', '/')
if i.isnumeric():
token[count] = int(i)
#token[count] = "[" + token[count] + "]"
count +=1
return token
strang = string_todictreference(string)
k = None
for i in strang:
if not k:
k = p[i]
else:
k = k[i]
print (i)
print(k)
,我想我实际上要说的是如何用实际字典值替换引用?
i'm used to xpath and Dom on php
using this but seams klunky
p is the json object from json.loads(file)
string="#/paths/~1golly~1gee/get/parameters/0"
def string_todictreference(string, p=None):
token = string.lstrip('#/').split('/')
count = 0
for i in token:
token[count] = i
if '~1' in i:
token[count] = i.replace('~1', '/')
if i.isnumeric():
token[count] = int(i)
#token[count] = "[" + token[count] + "]"
count +=1
return token
strang = string_todictreference(string)
k = None
for i in strang:
if not k:
k = p[i]
else:
k = k[i]
print (i)
print(k)
just trying out, i guess what I'm actually trying to say is how to replace references with the actual dictionary value?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
确保您卸载旧的“ JSONREF”版本,如果偶然安装
可能是一种更优雅的方式,但可以使其正常工作,以后会完善
make sure you uninstall old 'jsonref' version if install by accident
probably a more elegant way, but made it work, will refine later