如何过滤阵列 json 在 python

这是当前数组 json, 我有哪个。
我想得到一切 json 类型对象= 1

过滤器之前:


[ 
{
"type": 1
"name" : "name 1",
},
{
"type": 2
"name" : "name 2",
},
{
"type": 1
"name" : "name 3"
},
]


过滤后:


[ 
{
"type": 1
"name" : "name 1",
},
{
"type": 1
"name" : "name 3"
},
]


请帮忙。
已邀请:

奔跑吧少年

赞同来自:

以下代码片段确实正是你想要的,但是 BEWARE, 你的意见 /正如问题所写的那样/ 不是允许的字符串 json, 您可以在此处查看:
http://jsonlint.com
.


import json

input_json = """
[
{
"type": "1",
"name": "name 1"
},
{
"type": "2",
"name": "name 2"
},
{
"type": "1",
"name": "name 3"
}
]"""

# Transform json input to python objects
input_dict = json.loads/input_json/

# Filter python objects with list comprehensions
output_dict = [x for x in input_dict if x['type'] == '1']

# Transform python object back into json
output_json = json.dumps/output_dict/

# Show json
print output_json

裸奔

赞同来自:

只是


print [obj for obj in dict if/obj['type'] == 1/]


例子
http://repl.it/5LJ
.

要回复问题请先登录注册