【问题标题】:How to parse and print Nested array value from JSON format如何从 JSON 格式解析和打印嵌套数组值
【发布时间】:2016-01-11 01:09:57
【问题描述】:

我想在 JSON 下解析和打印,但打印时出现问题。

JSON 格式为:

{"categories": 
[
  "Firewall Permit","Custom Policy 1"
]
}

我曾经使用下面的代码来实现这一点,但我只能打印“防火墙许可”

>>> for x in json_data:
    line1 = (x['categories'][0])
    print (line1)

Firewall Permit

但我想打印如下:Firewall Permit, Custom Policy

请指导我。如何使用这个 JSON 字典。

【问题讨论】:

    标签: json python-3.x


    【解决方案1】:

    您可以使用 Python 3 print 函数的功能:

    for entry in json_data:
        print(*entry['categories'], sep=', ')
    

    打印:

    Firewall Permit, Custom Policy 
    

    【讨论】:

      【解决方案2】:

      你可以用这种简单的方式做到这一点:

      print(', '.join(json_data['categories']))
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-08-25
        • 1970-01-01
        • 2020-08-27
        • 1970-01-01
        • 1970-01-01
        • 2021-07-12
        相关资源
        最近更新 更多