简体   繁体   中英

Nested Json Lists formatting incorrectly

Ok, so I'm using an API. I'm trying to display a list that is returned by the api. The challenge is that I need to use .json to go through the response, but then it makes the list a json list and looks wrong.

checkList #is the return value
>>> checkList
u'{"list":["ad","ae"]}'
>>> str(checkList.json()['list'])
"[u'ad', u'ae']"

I'm using a python shell. How would I remove the " u' " from each element in the list? Thanks

The issue is not really in removing the u from the start of those strings. The easiest way to do this is to import the json module and call json.dumps(checklist.json()['list']) . It will do the right thing for you. The strings the json module returns are unicode objects (and are represented in the repr) as unicode literals. To "remove" them you need to handle the unicode strings better and this is the easiest way that will result in the least hair pulling and most forward compatibility with python 3.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM