如何使用python打印json文件中的特定值?

如何使用python打印json文件中的特定值?,python,linux,python-2.7,Python,Linux,Python 2.7,我刚开始使用python,我想知道如何使用python打印json文件中的一些值,下面是我的json文件 { "igt@gem_reloc_overflow@single-overflow": { "__type__": "TestResult", "command": "/home/gfx/intel-graphics/intel-gpu-tools/tests/gem_reloc_overflow --run-subtest single-overfl

我刚开始使用python,我想知道如何使用python打印json文件中的一些值,下面是我的json文件

{
    "igt@gem_reloc_overflow@single-overflow": {
        "__type__": "TestResult",
        "command": "/home/gfx/intel-graphics/intel-gpu-tools/tests/gem_reloc_overflow --run-subtest single-overflow",
        "dmesg": "",
        "environment": "PIGLIT_PLATFORM=\"mixed_glx_egl\" PIGLIT_SOURCE_DIR=\"/home/gfx/intel-graphics/intel-gpu-tools/piglit\"",
        "err": "(gem_reloc_overflow:19562) CRITICAL: Test assertion failure function reloc_tests, file gem_reloc_overflow.c:260:\n(gem_reloc_overflow:19562) CRITICAL: Failed assertion: __gem_execbuf(fd, &execbuf) == -14\n(gem_reloc_overflow:19562) CRITICAL: error: -22 != -14\nSubtest single-overflow failed.\n**** DEBUG ****\n(gem_reloc_overflow:19562) DEBUG: relocation_count=4294967295\n(gem_reloc_overflow:19562) CRITICAL: Test assertion failure function reloc_tests, file gem_reloc_overflow.c:260:\n(gem_reloc_overflow:19562) CRITICAL: Failed assertion: __gem_execbuf(fd, &execbuf) == -14\n(gem_reloc_overflow:19562) CRITICAL: error: -22 != -14\n****  END  ****\n",
        "exception": null,
        "out": "IGT-Version: 1.14-g1e9a3ac (x86_64) (Linux: 4.6.0-rc4-drm-intel-nightly-ww17-commit-1e81bac+ x86_64)\nStack trace:\n  #0 [__igt_fail_assert+0x101]\n  #1 [reloc_tests+0x6d6]\n  #2 [<unknown>+0x6d6]\nSubtest single-overflow: FAIL (8.469s)\n",
        "pid": 19562,
        "result": "fail",
        "returncode": 99,
        "subtests": {
            "__type__": "Subtests"
        },
        "time": {
            "__type__": "TimeAttribute",
            "end": 1462072402.5360818,
            "start": 1462072393.7328644
        },
        "traceback": null
    }
}

thaks

函数
json.load
返回一个字典(类型为
dict
的对象)

字典把键和字母联系起来。要访问字典中的值,可以使用以下语法:

value = dictionary[key]
在您的特殊情况下:

result = json_data['result']
这将打印结果

试一试

for key, value in json_data.iteritems():
    result = value['result']

print result
更新(针对评论中的问题): 如果您有多个文件,并且希望同时存储所有信息,请尝试将其全部放入字典中。这可能会因您希望密钥是什么而有所不同。但是试试这个(这将创建一个
{json\u key:result\u value}
:

all_results = {}
json_file_list = ['file_1.json', 'file_2.json']
for file in json_file_list:
    with open(file) as json_file:
        json_data = json.load(json_file)
        for key, value in json_data.iteritems():
            if 'result' in value:
                all_results[key] = value['result']
return all_results

json数据存储为python字典

因此,您可以访问“result”的值,如下所示:

json_data["igt@gem_reloc_overflow@single-overflow"]["result"]
W.R.T您的代码:

import json

with open("9.json") as json_file:
        json_data = json.load(json_file)


print(json_data["igt@gem_reloc_overflow@single-overflow"]["result"])

可能重复为什么这个问题带有linux标签?谢谢它对我有用,我不知道我是否可以再要求一件事,但是如果有可能的话,有什么方法可以改进这个脚本,我的意思是我在一个文件夹中有100个JSON,每个JSON都有不同的结果,可以对它们进行迭代,并将不同的结果存储在几个va中变量?结果将是:失败、通过、dmesg警告、dmesg失败、跳过、不完整。如果不可能,我将理解感谢为什么?请为代码添加一些上下文。
json_data["igt@gem_reloc_overflow@single-overflow"]["result"]
import json

with open("9.json") as json_file:
        json_data = json.load(json_file)


print(json_data["igt@gem_reloc_overflow@single-overflow"]["result"])