mie*_*zej 47 python json element count
如何获取JSON数据节点中的元素数量?
JSON:
{
"result":[
{
"run":[
{
"action":"stop"
},
{
"action":"start"
},
{
"action":"start"
}
],
"find":true
}
]
}
Run Code Online (Sandbox Code Playgroud)
我需要从节点获取元素的数量data['result'][0]['run'].它应该是3,但我无法在Python中找到它.
pnv*_*pnv 68
import json
json_data = json.dumps({
"result":[
{
"run":[
{
"action":"stop"
},
{
"action":"start"
},
{
"action":"start"
}
],
"find": "true"
}
]
})
item_dict = json.loads(json_data)
print len(item_dict['result'][0]['run'])
Run Code Online (Sandbox Code Playgroud)
在dict中转换它.
你很接近。一个非常简单的解决方案是从返回的“运行”对象中获取长度。无需担心“加载”或“加载”:
len(data['result'][0]['run'])
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
104357 次 |
| 最近记录: |