Skip to content

如何處理 JSON file 的key 裡有關鍵字 #2

@jarwow

Description

@jarwow

問題情境如下:
我利用 nodejs call api 後得到一個 json file,我需要拆解 json file 後再拼成一個新的 json,
問題在api回來給我的API其中有一個key & values 長得像這樣
"#text": “/dev/watson”

我在取”#text” values的值時以這樣的方法取:
console.log(secContents.es_result.ibmsc_field[myKey].#text);

都會得到以下error:
console.log(secContents.es_result.ibmsc_field[myKey].#text);

SyntaxError: Unexpected token ILLEGAL
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:373:25)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)

=================== 這是分隔線 ===================
[SOLUTION]

default
將code 從
console.log(secContents.es_result.ibmsc_field[myKey].#text);
改成
console.log(secContents.es_result.ibmsc_field[myKey]["#text"]);

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions