python取json数组的值
时间: 2023-08-29 20:12:49 浏览: 154
好的,关于取json数组的值,可以使用Python的json模块,先把json数据解析成Python对象,然后根据数组的下标来访问值,比如:
```json
{
"fruit": {
"apple": {
"color": "red",
"price": 1.2
},
"banana": {
"color": "yellow",
"price": 0.6
},
"orange": {
"color": "orange",
"price": 0.8
}
}
}
```
如果要访问apple的颜色,可以这样写:
```python
import json
data = '''
{
"fruit": {
"apple": {
"color": "red",
"price": 1.2
},
"banana": {
"color": "yellow",
"price": 0.6
},
"orange": {
"color": "orange",
"price": 0.8
}
}
}
'''
obj = json.loads(data)
print(obj['fruit']['apple']['color'])
```
输出结果为:red
阅读全文