我需要访问名称包含破折号的 JSON 数据,最好的解决方案是什么?删除破折号或只是处理它 这是我从简单 Node 服务器接收到的 JSON 数据。
[{
"id": "xxxxxxxxxxxxx",
"type": "container",
"path": "/",
"dctitle": "xxxx",
"dc-creator": "xxxx",
"dc-description": "xxxx.",
"dcrights": "xxxx",
"hero-region": {
"hero-headline": "xxxxxxxxxxxx",
"hero-headline-sub": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
}
}]
提前致谢!
最佳答案
根据我的经验,您不应在 json 键中使用破折号。这计算为减法:
dc - creator
我通常看到 -
被替换为 _
或转换为驼峰命名法。
看起来你可以 cmd-shift-L 替换 vsCode 中的所有破折号,或者你可以用下划线替换 10 个。但请注意,数据中不存在破折号。
要轻松转换为驼峰命名法,您可以使用这个强大的扩展:MD Tools
如果您坚持,那么您可以使用括号访问包含不能出现在标识符中的字符的 key :
["dc-creator"]
关于json - 如何在 Angular2 中访问名称中带有破折号的 JSON 数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41581951/