UTF8编码的字符在NodeJS上无法正确显示

时间:2017-04-26 07:34:56

标签: json encoding utf-8

当我打印以下内容时

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' ]

我不理解第三和第四个印刷语句之间的区别。 3中的变量不是包含与第4个相同的字符串吗?

已解决: Joe的评论解决了这个难题。第二个打印虽然用单个\打印变量实际上是双重转义,因此双重转义需要通过Joe的评论中建议的替换函数进行转换。

1 个答案:

答案 0 :(得分:1)

您实际上可以使用JSON.parse将其放入\x。您确定要解析字符串(而不是包含字符串的数组)吗?

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"]')
=> ["थडथडदय followed you", "Users", "233", "some_url", 201, "Users"]

VS

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"])
=> Uncaught SyntaxError: Unexpected token ,