asp c中的结构化数据JSON ld#

时间:2017-04-25 23:16:15

标签: c# json

我希望通过json ld将结构数据脚本添加到c#asp.net Web应用程序。

这是我想要添加的结构化数据脚本。由于与c#:The name 'context' does not exist in the current context冲突,我收到运行时错误...这显然是由于@语法冲突造成的。

有什么方法可以解决这个问题?

<script type="application/ld+json">
{
"@context": "http://schema.org",
"@type": "LegalService",
"image": "https://example.com.png",
"@id": "https://example.com",
"name": "Example Name",
"description": "Example description.",
"address": {
"@type": "PostalAddress",
"streetAddress": "Example Address",
"addressLocality": "Example City",
"addressRegion": "Example State",
"postalCode": "Example Postal",
"addressCountry": "Example Country"
},
"url": "https://example.com",
"telephone": "Example number",
"sameAs": [
"example social account 1",
"example social account 2"
]
}
</script>

所以看起来围绕这个问题的方法,感谢@scartag,是添加一个双@。

我也试图在脚本中添加一个循环,但是我遇到了运行时错误。我不确定这是否可行。它适用于将在CMS中生成的博客文章。

见下文:

<script type="application/ld+json">
{
  "@@context": "http://schema.org",
  "@@type": "NewsArticle",
  "mainEntityOfPage": {
    "@@type": "WebPage",
    "@@id": "https://google.com/article"
  },
  @foreach (var post in recent)
    {
  "headline": "Article headline",
  "image": {
    "@@type": "ImageObject",
    "url": "https://google.com/thumbnail1.jpg",
    "height": 800,
    "width": 800
  },
  "datePublished": "2015-02-05T08:00:00+08:00",
  "dateModified": "2015-02-05T09:20:00+08:00",
  "author": {
    "@@type": "Person",
    "name": "John Doe"
  },
   "publisher": {
    "@@type": "Organization",
    "name": "Google",
    "logo": {
      "@@type": "ImageObject",
      "url": "https://google.com/logo.jpg",
      "width": 600,
      "height": 60
    }
  },
  "description": "A most wonderful article"
}
}
</script>

1 个答案:

答案 0 :(得分:5)

修复json(删除多余的])后,如评论中所指示的那样。

您可以转义@标志

"@@context": "http://schema.org",