json - 如何在 Angular2 中访问名称中带有破折号的 JSON 数据?

标签 json node.js angular

我需要访问名称包含破折号的 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/

相关文章:

mysql - CakePHP 不保存长文本

node.js - grunt-cucumber 未运行 step_definitions

node.js - 如何将变量传递给 Express 中的 View ?

angular - 如何在共享服务中使用npm-xlsx实现导入Excel?

java - 通过 XSLT 将 Java 对象转换为 JSON

json - 无法从 AFNetworking 的 responseObject 解析 JSON

arrays - Postgres - 无法加入 UUID 上的 Json 数组值

mysql - 如何有效地检索同一个表中的相交值?

javascript - 允许 <a> 标签标题属性中包含 html

javascript - 如何将我点击的值传递到下一页