我在使用node.js。我從一個檔案中讀取一個物件,作為JSON使用。
vc_channels.json檔案:
{
"hangout-room"。"hangout-room is a voice and/or video room for anything sfw and not necessarily related to meditation." .
code to read:
fileContent = fs.readFileSync(' ./vc_channels.json')。
vc_channels = JSON.parse(fileContent)。
但是當我嘗試訪問:
console.log(vc_channels)。
console.log(channel.name)。
item.topic = vc_channels[channel.name] 。
console.log(item.topic)。
結果是未定義的: console.log
{
'hangout-room'。'hangout-room is a voice and/or video room for anything sfw and not necessarily related to meditation.'。
}
hangout-room
undefined
有沒有人知道我做錯了什么?
uj5u.com熱心網友回復:
如果我復制你的vc_channels.json的文本,我可以看到在"hangout-room"部分有一個u200b(零寬度空間):
在下面的片段中,我們可以看到第一個ASCII碼是200b,我們有68,之后是h等等:
const x = "hangout-room" ;
// This prints: [200b", "68", "61", "6e", "67", "6f", "75", "74", "2d", "72", "6f", "6d"]
console.log(x.split(''/span>)。 map(c => c. charCodeAt().toString(16) ));
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
因此,你只需從那里洗掉零寬度的空格字符。只要把這個的輸出復制到你的json檔案中。"hangout-room".replace('/200b', '') 或者使用任何可以顯示這種奇怪字符的文本編輯器,讓你洗掉它。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/307062.html
標籤:

