下面是我拥有的JSON数据,我只想获得名称,after_count。
{
"total_count": 125,
"limit": 100,
"operatingsystems": [
{
"category": null,
"name": "abc",
"total_count": 1,
"notes": "",
"after_count": 11,
"id": 1,
"aliases": []
},
{
"category": null,
"name": "cdef",
"total_count": 2,
"notes": "",
"after_count": 62,
"id": 2,
"aliases": []
},
我的代码:
data = [item for item in objects if item["after_count"] >= 0]
我得到的输出:
"
a
l
i
a
s
e
s
"
:
[
]
有人可以指导我朝正确的方向发展。
答案 0 :(得分:1)
我在你的问题中为字符串添加了几个字符,这样json的语法就会完整,然后填入json.loads
来创建一个json对象。
s = '''{
"total_count": 125,
"limit": 100,
"operatingsystems": [
{
"category": null,
"name": "abc",
"total_count": 1,
"notes": "",
"after_count": 11,
"id": 1,
"aliases": []
},
{
"category": null,
"name": "cdef",
"total_count": 2,
"notes": "",
"after_count": 62,
"id": 2,
"aliases": []
}]}'''
import json
j = json.loads(s)
print (j["operatingsystems"][0]["name"])
print (j["operatingsystems"][0]["after_count"])
print (j["operatingsystems"][1]["name"])
print (j["operatingsystems"][1]["after_count"])
最后四个陈述显示了如何访问所需的项目。