2017-04-26 106 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