Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 尝试获取JSON响应值时获取字符串而不是列表_Python_Django - Fatal编程技术网

Python 尝试获取JSON响应值时获取字符串而不是列表

Python 尝试获取JSON响应值时获取字符串而不是列表,python,django,Python,Django,我有以下JSON响应: {"response": [100, {"name": "Bill"}, {"name": "John"}]} 我所需要的就是遍历列表。 因此,我的计划是首先获取列表,然后遍历它。 但是当我想和你一起去拿名单的时候 list_dict.json().get("response") 我得到了线索: 100{"name": "Bill"}{"name": "John"} 我怎样才能得到这份名单 更新:以下是相关的代码视图.py from django.http impo

我有以下JSON响应:

{"response": [100, {"name": "Bill"}, {"name": "John"}]}
我所需要的就是遍历列表。 因此,我的计划是首先获取列表,然后遍历它。 但是当我想和你一起去拿名单的时候

list_dict.json().get("response")
我得到了线索:

100{"name": "Bill"}{"name": "John"}
我怎样才能得到这份名单

更新:以下是相关的代码视图.py

from django.http import HttpResponse
from lib.api import Api
import requests, json

def verify(request):
    api = Api(access_token=access_token)
    list_dict = api.get_all(owner_id=owner_id)
    result = list_dict.json().get("response")
    return HttpResponse(result)
这是api.py

import requests
class Api:
    def __init__(self, access_token='', **kwargs):
        self.access_token = access_token

    def get_all(self, owner_id=''):
        api_url_template = 'http://api.example.com/method/get.All?owner_id={0}&access_token={1}'
        api_url = api_url_template.format(owner_id, self.access_token)
        response = requests.get(api_url)
        return response

这只是将列表传递给HttpResponse的结果。如果要将完整响应视为字符串,请将其作为字符串传递:

return HttpResponse(str(result))

当然,要将其用作代码中的列表,不需要将其转换为字符串,只需按原样使用即可。

json()的返回结果是什么类型的。。。。似乎工作正常什么是
list\u dict
?@DanielRoseman list\u dict只是一个存储响应的变量,但它显然有一个
json()
方法。从哪里来?