다음 JSON 응답이 있습니다.
{"response": [100, {"name": "Bill"}, {"name": "John"}]}
필요한 것은 목록을 반복하는 것입니다. 그래서 내 계획은 먼저 목록을 얻은 다음 반복하는 것입니다. 하지만 내가 목록을 얻으려고 할 때
list_dict.json().get("response")
나는 문자열을 얻었다 :
100{"name": "Bill"}{"name": "John"}
목록을 어떻게 얻을 수 있습니까?
업데이트 : 다음은 관련 코드 views.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))
물론 코드 내에서 목록으로 사용하기 위해 문자열로 변환 할 필요가 없습니다. 그대로 사용할 수 있습니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다