python - Python请求访问响应对象的索引
问题描述
我是 python 新手,正在尝试从 api 调用中聚合数据
我有这个脚本
r = requests.get('https://jsonplaceholder.typicode.com/users')
print r.text
它以这种格式返回一个对象数组
[{
"id": 1,
"name": "Leanne Graham",
"username": "Bret",
"email": "Sincere@april.biz",
"address": {
"street": "Kulas Light",
"suite": "Apt. 556",
"city": "Gwenborough",
"zipcode": "92998-3874",
"geo": {
"lat": "-37.3159",
"lng": "81.1496"
}
}]
我一直在玩,并尝试了这个,看看我是否可以访问第一个对象
print r.text[0]
它没有用。那么我该如何用python做到这一点
解决方案
您需要解析 JSON 文本:
import json
array = json.loads(r)
print array[0]
推荐阅读
- powershell - 安装-WindowsFeature 返回错误:0x80040150
- firebase - 为公共访问创建第二个 Firebase 项目是否更安全?
- sql-server - 基于标识列插入然后更新表的存储过程
- reporting-services - 缺少父和子报表参数框
- android - 我可以使用 WebView 进行 Auth0 登录吗?
- python - 如何在字符串中搜索包括 \n 通过 re.search
- ruby-on-rails - 如何防止用户只输入 /.../model/model_id: 在 url 中访问其他用户在 Ruby on Rails 中的记录?
- python - 为什么我不想立即将所有可迭代对象转换为具有定义长度的对象(例如列表)?
- imputation - 当您有多个变量对具有缺失值的列不重要时,如何估算?
- javascript - 仅显示二维数组中的第一个元素