Json object returned by HTTP response

I use python and django to develop some REST APIs. I have a question about the unicode JSON string returned by a query call. So, I am doing something like:

resp = requests.get(self.url)
if resp.status_code is status.HTTP_200_OK:
    obj = json.loads(resp.json())

With this, I can iterate over entries like:

for o in obj:
    print o

It will print something like:

{u'pk': 1, u'model': u'aslapp.imagetypemodel', u'fields': {u'type': u'PNG'}}
{u'pk': 2, u'model': u'aslapp.imagetypemodel', u'fields': {u'type': u'JPG'}}

However, I read that the call resp.json()must call this method json.loads()internally and will take care of the encoding. However, simply:

obj = resp.json()
for o in obj:
    print o

It simply repeats every character in a unicode string. So, should I run it again using the method loads, if I want to iterate over JSON entries? What will be the correct way to repeat all the JSON entries returned with resp.json().

+4
1

, resp.json() json.loads() .

, resp.json() , , API json . , "{\"pk\": 1}" {"pk": 1}.

API, , Python.

+5

Source: https://habr.com/ru/post/1661489/


All Articles