简体   繁体   English

嵌套的Json列表格式错误

[英]Nested Json Lists formatting incorrectly

Ok, so I'm using an API. 好的,所以我正在使用API​​。 I'm trying to display a list that is returned by the api. 我正在尝试显示由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. 面临的挑战是,我需要使用.json来进行响应,但随后使该列表成为json列表,并且看起来不对。

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

I'm using a python shell. 我正在使用python shell。 How would I remove the " u' " from each element in the list? 如何从列表中的每个元素中删除“ u”? Thanks 谢谢

The issue is not really in removing the u from the start of those strings. 问题并不是从这些字符串的开头删除u The easiest way to do this is to import the json module and call json.dumps(checklist.json()['list']) . 最简单的方法是导入json模块并调用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. json模块返回的字符串是unicode文字作为unicode对象(并在repr中表示)。 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. 要“删除”它们,您需要更好地处理unicode字符串,这是最简单的方法,它将使头发的牵扯最少,并且与python 3的兼容性最高。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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