json2html 如何处理json键中的特殊字符(例如@)

json2html how to deal with special characters (e.g. @) in json keys

RESTful API 返回的 JSON 响应包含某些键的 @ 符号,其他键以这种方式生成 ("document.title")。

我的作品JSON是这样的

"fields": {
  "document.content_type": [
    "application/ms-word"
  ],
  "document.name": [
    "zh1.docx"
  ],
  "@title": [
    "The History of the Pencil"
  ],
  "@date": [
    "2016-01-13T07:30:25-0500"
  ],
  "document.content": [
    "some text goes here"
  ],
  "@guid": [
    "76c99131-23b1-4435-9b93-eaabd9e33a67"
  ]
}

在正常 JavaScript/jQuery 中,我可以轻松访问这些值,方法是执行字段["@title"][0] 获取标题或执行字段["document.content"][0] 获取文档内容,但这种格式在 json2html 转换中不起作用。

例如,转换中的这段代码不起作用。

{"tag":"h4","html":"${fields['document.name'].0}"}

谁能告诉我如何在 json/html 转换中访问那些特殊的 json 标签。我知道在某些情况下我可以将标签更改为更标准的格式,但如果我无法更改它们或出于某种原因它们需要保持原样怎么办?

json2html 转换将 ${} 内的字符串拆分为“.”。您可以更改键以与转换兼容,例如:

for(var k in data.fields){
  if(k.match(/\./)){
    data.fields[k.replace(/\./g, '_')] = data.fields[k];
  }
}

Plunk