UTF8编码的字符未正确显示在Nodejs上



当我打印以下

console.log('xe0xa4xa5xe0xa4xa1xe0xa4xa5xe0xa4xa1xe0xa4xa6xe0xa4xaf followed you')
console.log(utf8.decode('xe0xa4xa5xe0xa4xa1xe0xa4xa5xe0xa4xa1xe0xa4xa6xe0xa4xaf followed you'))

正确的结果是

थडथडदय followed you - (i)
थडथडदय followed you            - (ii)

当我使用redis.lrange('notif-'+userId, 0, -1)访问REDIS列表时,其第一个元素显示为

["xe0xa4xa5xe0xa4xa1xe0xa4xa5xe0xa4xa1xe0xa4xa6xe0xa4xaf 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))时,打印的内容是

xe0xa4xa5xe0xa4xa1xe0xa4xa5xe0xa4xa1xe0xa4xa6xe0xa4xaf followed you
xe0xa4xa5xe0xa4xa1xe0xa4xa5xe0xa4xa1xe0xa4xa6xe0xa4xaf 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('["xe0xa4xa5xe0xa4xa1xe0xa4xa5xe0xa4xa1xe0xa4xa6xe0xa4xaf followed you", "Users", "233", "some_url", 201, "Users"]'))

结果是

['xe0xa4xa5xe0xa4xa1xe0xa4xa5xe0xa4xa1xe0xa4xa6xe0xa4xaf followed you', 'Users', '233', 'https://storage.googleapis.com/humbee_images/cartoon-bee-1.jpg', 201, 'Users']
["xe0xa4xa5xe0xa4xa1xe0xa4xa5xe0xa4xa1xe0xa4xa6xe0xa4xaf 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' ]

我不了解第三和第四印刷语句之间的区别。是否包含与第4个字符串相同的字符串?

中的3个变量不是吗?

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

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

JSON.parse('["xe0xa4xa5xe0xa4xa1xe0xa4xa5xe0xa4xa1xe0xa4xa6xe0xa4xaf followed you", "Users", "233", "some_url", 201, "Users"]')
=> ["थडथडदय followed you", "Users", "233", "some_url", 201, "Users"]

vs。

JSON.parse(["xe0xa4xa5xe0xa4xa1xe0xa4xa5xe0xa4xa1xe0xa4xa6xe0xa4xaf followed you", "Users", "233", "some_url", 201, "Users"])
=> Uncaught SyntaxError: Unexpected token ,

最新更新