【问题标题】:UTF8 encoded characters not displaying properly on NodeJSUTF8 编码的字符无法在 NodeJS 上正确显示
【发布时间】: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包含相同的字符串吗?

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

【问题讨论】:

    标签: json encoding utf-8


    【解决方案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"]
    

    对比

    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 ,
    

    【讨论】:

    • let notificationList = JSON.parse(notificationParent.replace(/\\/g, '\\\\')) 中,如果我传递了一个包含字符串的数组,我是否能够调用.replace()。因此,我不能确定 notificationParent 和替换后的结果确实是一个字符串吗?我尝试了let notificationList = JSON.parse(notificationParent.toString()),并在位置 3 的 JSON 中得到了 Unexpected token x。
    • 我从stackoverflow.com/questions/27059765/…的答案中了解到\x 是不允许的
    • 其他一些答案建议用 \u00 替换 \x。这也无济于事,无论如何该解决方案对我来说是不可行的,因为上面的 some_url 部分还包含 \x ,我不想打扰它。
    • JSON.parse 甚至不应该看到\x,因为它会在进入函数之前被替换:console.log('\xe0') => à vs. console.log('\xe' + '0') => SyntaxError: Invalid hexadecimal escape sequence
    • 如果您的字符串包含双斜杠,可以这样取消转义:'\\xe0\\xa4\\xa5\\xe0\\xa4\\xa1\\xe0\\xa4\\xa5\\xe0\\xa4\\xa1\\xe0\\xa4\\xa6\\xe0\\xa4\\xaf followed you'.replace(/\\x([0-9a-f]{2})/g, function(_, pair) { return String.fromCharCode(parseInt(pair, 16)); }) => "थडथडदय followed you"
    猜你喜欢
    • 2013-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-08
    • 2013-10-05
    • 1970-01-01
    • 2021-09-17
    • 2021-01-21
    相关资源
    最近更新 更多