代码之家  ›  专栏  ›  技术社区  ›  eymentakak

json字典类型错误:字符串索引必须是整数

  •  1
  • eymentakak  · 技术社区  · 2 年前
    import requests
        url = "**************"
    
    querystring = {"domain":"gmail.com","username":"random","server":"server-1","type":"real"}
    
    headers = {
        "X-RapidAPI-Key": "******************",
        "X-RapidAPI-Host": "*****************"
    }
    
    response = requests.request("GET", url, headers=headers, params=querystring).text
    

    答复:

    {"code":200,"msg":"OK","items":{"email":"vinhvivyanvinh72943@gmail.com","timestamp":1659215390}}
    

    .

    print(response['items'])
    
    TypeError: string indices must be integers
    

    当我在另一本字典中使用它时,我没有遇到任何问题。 如何将电子邮件和时间戳输入变量?

    1 回复  |  直到 2 年前
        1
  •  0
  •   Andrej Kesely    2 年前

    .json() 应请求响应:

    response = requests.request(
        "GET", url, headers=headers, params=querystring
    ).json()                                             # <-- note the .json()
    
    print(response["items"]["email"], response["items"]["timestamp"])