使用正斜杠NodeJS访问JSON密钥

时间:2019-05-09 20:19:51

标签: javascript node.js json

我有一个物体。在我的对象中,有一个带正斜杠的钥匙,我想访问带正斜杠pattern

的钥匙的孩子

我尝试了几种无效的方法,请提供访问pattern属性的解决方案。对象在底部

test.attributes./worker/workAssignment/homeOrganizationalUnits/typeCode/codeValue


SyntaxError: Unexpected token /
key = '/worker/workAssignment/homeOrganizationalUnits/typeCode/codeValue'
test.attributes.key


undefined
test.attributes.["key"]

Thrown:
SyntaxError: Unexpected token [
test.attributes.\/worker\/workAssignment\/homeOrganizationalUnits\/typeCode\/codeValue

Thrown:
SyntaxError: Invalid or unexpected token

{
    "attributes": {
        "/worker/workAssignment/homeOrganizationalUnits/typeCode/codeValue": {
            "pattern": "Department"
        }
    },
    "value": {
        "links": [{
            "href": "/codelists/hr/v3/worker-management/departments/WFN/1?$filter=foreignKey eq {payrollGroupCode}",
            "mediaType": "application/json",
            "method": "GET"
        }]
    }
}

2 个答案:

答案 0 :(得分:1)

host.RunAsCustomService(); var pathToExe = Process.GetCurrentProcess().MainModule.FileName; var pathToContentRoot = Path.GetDirectoryName(pathToExe); Directory.SetCurrentDirectory(pathToContentRoot); CreateWebHostBuilder(args) .Build() .RunAsService(); 都很好。

答案 1 :(得分:1)

您可以在键名中使用'/'字符进行对象访问,只要它是用于查找的变量的值即可。

var key = '///';
var obj = { '///': 'foo' };
obj[key];

> 'foo'

有关更多信息,请查看Property Accessors

上的MDN文档