Python JSON通过字符串路径获取值

发布于 2025-01-16 12:54:09 字数 588 浏览 2 评论 0原文

我有一个 JSON 文件,我想自动检查特定值, 就像: myJSON['generic']['lessgeneric']['store'] 应该等于 100

我正在考虑做这样的事情:

checks = [
    {'name':'field_1','path':'myJSON['generic']['lessgeneric']['store']','value':'100'}
]

我没有提示如何将字符串 "myJSON['generic']['lessgeneric']['store']" 转换为其值。

编辑: 我是怎么解决的

path = "generic>lessgeneric>store"
value = 100
def check_value(path,value):
    temp_json = my_json
    for key in path.split(">"):
        temp_json = temp_json[key]
    if temp_json == value:
        pass

I have a JSON file that I want to automatically check specific values,
like: myJSON['generic']['lessgeneric']['store'] should be equal to 100,

I was thinking of doing something like this:

checks = [
    {'name':'field_1','path':'myJSON['generic']['lessgeneric']['store']','value':'100'}
]

I have no clue how to convert the string "myJSON['generic']['lessgeneric']['store']" into it's value.

edit:
how I solved it

path = "generic>lessgeneric>store"
value = 100
def check_value(path,value):
    temp_json = my_json
    for key in path.split(">"):
        temp_json = temp_json[key]
    if temp_json == value:
        pass

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

染火枫林 2025-01-23 12:54:09

我建议您将其存储为列表或分隔字符串,而不是像这样存储路径。例如:

checks = [ {'name': 'field_1', 'path': 'generic/lessgeneric/store', 'value': 100} ]

然后,您可以使用 str.split() 拆分 path 键的值,并深入到您的对象。下面的递归函数接受一个对象(我们希望它是一个字典)和一个包含所有要深入研究的键的列表,并返回最终键处的值:

def drill_down(obj, path):
    if len(path) == 1: 
        # if path has a single element, return that key of the dict
        return obj[path[0]] 
    else:
       # Take the key given by the first element of path. Then drill down into it
       return drill_down(obj[path[0]], path[1:]) 

然后,执行:

for c in checks:
    path = c['path'].split('/')
    assert drill_down(myJSON, path) == c['value']

这假设您已经解析了 json使用 json 模块将其转换为 myJSON 变量中的对象。

Instead of storing the path like that, I suggest you store it as a list or as a delimited string. For example:

checks = [ {'name': 'field_1', 'path': 'generic/lessgeneric/store', 'value': 100} ]

Then, you can split the value of the path key with str.split(), and drill down into your object. The following recursive function takes an object (that we expect to be a dictionary) and a list containing all the keys to drill down into, and returns the value at the final key:

def drill_down(obj, path):
    if len(path) == 1: 
        # if path has a single element, return that key of the dict
        return obj[path[0]] 
    else:
       # Take the key given by the first element of path. Then drill down into it
       return drill_down(obj[path[0]], path[1:]) 

Then, do:

for c in checks:
    path = c['path'].split('/')
    assert drill_down(myJSON, path) == c['value']

This assumes you've already parsed your json into an object in the myJSON variable using the json module.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文