【问题标题】:Accessing a json object nested in a json array with Python 3.x使用 Python 3.x 访问嵌套在 json 数组中的 json 对象
【发布时间】:2017-05-19 01:53:11
【问题描述】:

鉴于下面的 json 有效负载,我如何使用 Python 3.x 获取“热点”的值?顶层似乎是一个带有一个键值对的字典。 'Recs' 是键,值是 Python 列表。我已经使用 json.loads(payload) 将 json 有效负载加载到 Python 类中。

json 负载:

{
    'Recs': [{
        'eSrc': 'big-a1',
        'reqPs': {
            'srcIP': '11.111.11.111'
        },
        'a1': {
            'a1Ver': '1.0',
            'obj': {
                'eTag': '38f028e',
                'sz': 1217,
                'seq': '02391D2',
                'hotspot': 'web/acme/srv/dev/8dd'
            },
            'confId': 'acme-contains',
            'pipe': {
                'name': 'acme.dev',
                'oId': {
                    'pId': 'BDAD'
                }
            }
        }
    }]
}

【问题讨论】:

    标签: python json list python-3.x dictionary


    【解决方案1】:

    { 表示一个字典,[ 表示一个列表,所以hotspot 位于:

    my_json['Recs'][0]['a1']['obj']['hotspot']
    

    【讨论】:

    • ['Recs'] 和 ['a1'] 之间的 [0] 是什么?
    • Recs[ 开头所以它是一个列表,[0] 选择列表的第一个元素
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-05
    • 1970-01-01
    相关资源
    最近更新 更多