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

时间:2018-03-22 19:29:46

标签: json python-2.7 jboss-cli

我创建了一个python脚本来从jboss-cli.sh中提取值,默认情况下以下面的格式返回输出:


    {
        "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但没有工作。

2 个答案:

答案 0 :(得分:0)

试试这个!

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

答案 1 :(得分:0)

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

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