Json UTF8编码字符未在NodeJ上正确显示

Json UTF8编码字符未在NodeJ上正确显示,json,encoding,utf-8,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')) 结果是正确的 थडथडदà¤

当我打印以下内容时

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相同的字符串吗


解决:乔的评论解决了这个难题。尽管使用单\打印变量,但第二次打印实际上是双转义的,因此双转义需要通过Joe注释中建议的替换函数进行转换。

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

vs


let notificationList=JSON.parse(notificationParent.replace(/\\/g,\\\\\'))
中,如果我传递了一个包含字符串的数组,我是否能够调用
.replace()
。因此,我不能确保
notificationParent
和替换后的结果确实是一个字符串吗?我尝试了
让notificationList=JSON.parse(notificationParent.toString())
并在JSON中的位置3处获得了意外的标记x。我知道,在建议用\u00替换\x的某个其他答案的某个地方,不允许使用\x。这也没有帮助,而且无论如何,这个解决方案对我来说是不可行的,因为上面的some_url部分也包含了\x,我不想打扰它。
JSON.parse
甚至不应该看到
\x
,因为它将在进入函数之前被替换:
console.log('\xe0')=>与
console.log('\xe'+0'))=>语法错误:无效的十六进制转义序列
如果字符串包含双斜杠,可以按如下方式取消转义:
\\xe0\\xa4\\xa1\\xe0\\xa4\\xa5\\xe0\\xa4\\xa1\\xe0\\xa4\\xa1\\xe0\\xa4\\xa6\\xe0\\xa4\\xaf。替换(/\\x([0-9a-f]{2})/g,函数({u0,pair){return string.fromCharCode(parseInt,pair,16))=>“跟着你走”
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 ,