【发布时间】:2017-04-26 07:34:55
【问题描述】:
当我打印以下内容时
console.log('\xe0\xa4\xa5\xe0\xa4\xa1\xe0\xa4\xa5\xe0\xa4\xa1\xe0\xa4\xa6\xe0\xa4\xaf followed you')
console.log(utf8.decode('\xe0\xa4\xa5\xe0\xa4\xa1\xe0\xa4\xa5\xe0\xa4\xa1\xe0\xa4\xa6\xe0\xa4\xaf followed you'))
正确的结果是
थडथडदय followed you - (i)
थडथडदय followed you - (ii)
当我使用 redis.lrange('notif-'+userId, 0, -1) 访问 redis 列表时,它的第一个元素显示为
["\xe0\xa4\xa5\xe0\xa4\xa1\xe0\xa4\xa5\xe0\xa4\xa1\xe0\xa4\xa6\xe0\xa4\xaf followed you", "Users", "233", "some_url", 201, "Users"]
(请注意,上面是使用 redis.lpush('notif-'+userId, python-list) 以字符串形式存储在 redis 中的列表,它是 redis 列表的第一项)
由于 \x 无法将上述内容放入 JSON.parse 中,我将斜杠转义,然后使用
还原let notificationList = JSON.parse(notificationParent.replace(/\\/g, '\\\\'))
notification.text = notificationList[0].replace(/\\\\/g, '\\')
现在,当我 console.log(notification.text) 和 console.log(utf8.decode(notification.text)) 时,打印出来的是
\xe0\xa4\xa5\xe0\xa4\xa1\xe0\xa4\xa5\xe0\xa4\xa1\xe0\xa4\xa6\xe0\xa4\xaf followed you
\xe0\xa4\xa5\xe0\xa4\xa1\xe0\xa4\xa5\xe0\xa4\xa1\xe0\xa4\xa6\xe0\xa4\xaf followed you
我应该怎么做才能获得类似于 (i) 和 (ii) 的结果?
编辑:从一开始,如果我执行以下操作
console.log(notificationParent)
notificationParent = notificationParent.replace(/'/g, '"');
console.log(notificationParent)
let notificationList = JSON.parse(notificationParent.toString())
console.log(notificationList)
console.log(JSON.parse('["\xe0\xa4\xa5\xe0\xa4\xa1\xe0\xa4\xa5\xe0\xa4\xa1\xe0\xa4\xa6\xe0\xa4\xaf followed you", "Users", "233", "some_url", 201, "Users"]'))
结果是
['\xe0\xa4\xa5\xe0\xa4\xa1\xe0\xa4\xa5\xe0\xa4\xa1\xe0\xa4\xa6\xe0\xa4\xaf followed you', 'Users', '233', 'https://storage.googleapis.com/humbee_images/cartoon-bee-1.jpg', 201, 'Users']
["\xe0\xa4\xa5\xe0\xa4\xa1\xe0\xa4\xa5\xe0\xa4\xa1\xe0\xa4\xa6\xe0\xa4\xaf followed you", "Users", "233", "https://storage.googleapis.com/humbee_images/cartoon-bee-1.jpg", 201, "Users"]
Syntax error: Unexpect token x in position 3
[ 'थडथडदय followed you',
'Users',
'233',
'some_url',
201,
'Users' ]
我不明白第三个和第四个打印语句之间的区别。 3rd中的变量不是和4th包含相同的字符串吗?
已解决:乔的评论解决了这个难题。第二个打印虽然用单个 \ 打印变量实际上是双重转义,所以双重转义需要通过乔的评论中建议的替换函数进行转换。
【问题讨论】: