使用python解析JSON,根据条件获取值

2024-05-13 08:53:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我是python新手,尝试解析json文件并根据条件获取所需字段。在

例如,如果status=true,那么 印刷体姓名

Json文件:

[
  {
    "id": "12345",
    "name": "London",
    "active": true,
    "status": "true",
    "version": "1.0",
    "tags": [
    ]
  },
  {
    "id": "12457",
    "name": "Newyork",
    "active": true,
    "status": "false",
    "version": "1.1",
    "tags": [
    ]
  },
]

预期产量:

名称:伦敦

请帮我这个忙。提前谢谢你。在


Tags: 文件nameidjsontrueversionstatustags
2条回答

您可以在这里获得关于json解析的所有信息。在

http://docs.python.org/3.3/library/json.html

>>> import json
>>> obj = json.loads('[ { "id": "12345", "name": "London", "active": true, "status": "true", "version": "1.0", "tags": [ ] }, { "id": "12457", "name": "Newyork", "active": true, "status": "false", "version": "1.1", "tags": [ ] } ]')
>>> print "Names:", ",".join(x["name"] for x in obj if x["status"] == "true")
Names: London

您的JSON无效。删除逗号如下:

^{pr2}$

相关问题 更多 >