从 python 中的变量而不是文件中读取 json 数据

reading json data from variable instead of file in python

我正在创建一个 python 脚本来从 jboss-cli.sh 中提取值,默认情况下 return 输出以下格式:


    {
        "outcome" => "success",
        "result" => {
            "ActiveCount" => "0",
            "AvailableCount" => "0",
            "AverageBlockingTime" => "0",
            "AverageCreationTime" => "0",
            "CreatedCount" => "0",
            "DestroyedCount" => "0",
            "InUseCount" => "0",
            "MaxCreationTime" => "0",
            "MaxUsedCount" => "0",
            "MaxWaitCount" => "0",
            "MaxWaitTime" => "0",
            "TimedOut" => "0",
            "TotalBlockingTime" => "0",
            "TotalCreationTime" => "0",
            "statistics-enabled" => false
        }
    }

现在我使用以下代码将其转换为 JSON 格式:

from jbossply.jbossparser import JbossParser parser = JbossParser() print(parser.parse(OUT))

所以输出为:

{u'outcome': u'success', u'result': {u'AverageBlockingTime': u'0', u'AvailableCount': u'0', u'statistics-enabled': False, u'MaxCreationTime': u'0', u'MaxUsedCount': u'0', u'CreatedCount': u'0', u'MaxWaitCount': u'0', u'TimedOut': u'0', u'InUseCount': u'0', u'ActiveCount': u'0', u'TotalBlockingTime': u'0', u'DestroyedCount': u'0', u'AverageCreationTime': u'0', u'TotalCreationTime': u'0', u'MaxWaitTime': u'0'}}

现在如何从中提取 "AvailableCount"。已尝试 json.load 但无法正常工作。

试试这个!

from jbossply.jbossparser import JbossParser
import json

parser = JbossParser()
parser_out = parser.parse(OUT)
json_dump = json.dump(parser_out)
json_load = json.loads(json_dump)
print json_load['result']['AvailableCount']
# op 0

最后我自己得到了答案,将 json 解析为一个变量并提取结果,如 beow:

parser = JbossParser() data=(parser.parse(OUT)) TOTAL_CN=(data["result"]["AvailableCount"])