在Python2中将False转换为\ u0000,在True中转换为\ u0001

问题描述 投票:0回答:1

我正在Python2中打印像JSON这样的字典,我正在使用print(json.dumps(request_dr.data['data'])),但现在控制台的输出是:

{
   "id": 711,
   "username": "esteban@gtt",
   "first_name": "esteban@gtt",
   "last_name": "",

    ... Anothers fields

   "passwordChangedOnce": "\u0001",
   "ldapCheck": "\u0000"
 }

所以,我如何将passwordChangedOnce转换为true并将ldapCheck转换为false,以便得到如下所示:

{
   "id": 711,
   "username": "esteban@gtt",
   "first_name": "esteban@gtt",
   "last_name": "",

    ... Anothers fields

   "passwordChangedOnce": "true",
   "ldapCheck": "false"
 }

我已经阅读了Stack Overflow中的另一个答案,但是没有成功。谢谢

python python-2.x
1个回答
0
投票

对此没有什么幻想或自动的,如果您想对值进行搜索和替换,编写对值进行搜索和替换的代码

def convertDict(d):
    for (k, v) in d.items():
        if v == '\x00':   # this is the same string that json serializes as "\u0000"
            d[k] = False
        elif v == '\x01': # this is the same string that json serializes as "\u0001"
            d[k] = True
    return d

print(json.dumps(convertDict(request_dr.data['data'])))
© www.soinside.com 2019 - 2024. All rights reserved.