Python请求访问响应对象的索引

Python请求访问响应对象的索引,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": {

我是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]

您需要解析JSON文本:

import json
array = json.loads(r)
print array[0]

text返回Http响应正文。 因此,如果您想获得json的第一个属性,
您应该将字符串转换为json对象

这很有效

result = r.text
print(type) # prints str

import json
result = json.loads(result)

print(result[0]) # (...)

text返回Http响应正文。 因此,如果您想获得json的第一个属性,
您应该将字符串转换为json对象

这很有效

result = r.text
print(type) # prints str

import json
result = json.loads(result)

print(result[0]) # (...)

打印r.text[0]
是否返回任何内容?(空格?括号?)它不是返回数组,而是返回JSON字符串。您需要使用
json.loads()
将其解析为数组。
print r.text[0]
是否返回任何内容?(空格?括号?)它不是返回数组,而是返回JSON字符串。您需要使用
json.loads()
将其解析为数组。