2017-04-26 138 views
0

当我打印正确显示以下UTF8编码字符不上的NodeJS

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中的字符串,使用红色is.lpush('notif - '+ userId,python-list),它是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' ] 

我不明白的第三和第四个print语句之间的区别。第三个变量是否包含与4th相同的字符串?

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

回答

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 , 
+0

在'让notificationList = JSON.parse(notificationParent.replace(/ \\ /克, '\\\\'))',曾我被传递数组包含一个字符串,我是否能够调用'.replace()'。不管我是否确定'notificationParent'和替换后的结果确实是一个字符串?我尝试了'让notificationList = JSON.parse(notificationParent.toString())',并在JSON中获得了位置3处的意外令牌x。 – user3740387

+0

我知道\ x不允许从http://stackoverflow.com上的答案/ questions/27059765/node-js-json-parse-utf-8-string – user3740387

+0

其他一些回答建议将\ x替换为\ u00。这并没有太大的帮助,无论如何,这个解决方案对我来说不可行,因为上面的some_url部分也包含\ x,我不想打扰它。 – user3740387