小编Rak*_*mar的帖子

如何使用 Python 从内部具有多个层次结构的 json 中提取值

以下是json内容,如何使用python提取“GBL_ACTIVE_CPU”的值。

{
        "test": "00.00.004",
        "Metric Payload": [
            {
                "ClassName": "test",
                "SystemId": "test",
                "uri": "http://test/testmet",
                "MetaData": [
                    {
                        "FieldName": "GBL_ACTIVE_CPU",
                        "DataType": "STRING",
                        "Label": "test",
                        "Unit": "string"
                    }
                ],
                "Instances": [
                    {
                        "InstanceNo": "0",
                        "GBL_ACTIVE_CPU": "4"
                    }
                ]
        ]               
}
Run Code Online (Sandbox Code Playgroud)

我试过下面的代码,但不起作用。任何帮助表示赞赏:

result = json.loads(jsonoutput)
print(result)
node = result["Metric Payload"]["Instances"]["GBL_ACTIVE_CPU"]
print(node)
Run Code Online (Sandbox Code Playgroud)

我得到以下错误:

TypeError: list indices must be integers or slices, not str
Run Code Online (Sandbox Code Playgroud)

python json extract

1
推荐指数
1
解决办法
632
查看次数

标签 统计

extract ×1

json ×1

python ×1