【问题标题】:json-ld: Good way to model custom valuesjson-ld:建模自定义值的好方法
【发布时间】:2017-04-27 14:06:08
【问题描述】:

我正在尝试获得一个很好的 json-ld,它将 schema.org/Product 定义与一些自定义元素相结合。

我来自xsd背景,json-ld的可扩展性似乎很难实现。

我从在 Google (https://developers.google.com/search/docs/guides/search-gallery) 上找到的产品模板标记开始并尝试对其进行扩展(我想添加类似 mydomain:tags 的内容),但我不知道该怎么做。

<script type="application/ld+json">
{
  "@context": ["http://schema.org/",
    {"mydomain": "http://mystuff.com/"}],
  "@type": "Product",
  "name": "Executive Anvil",
  "image": "http://www.example.com/anvil_executive.jpg",
  "description": "Sleeker than ACME's Classic Anvil, the Executive Anvil is perfect for the business traveler looking for something to drop from a height.",
  "mpn": "925872",
  "brand": {
    "@type": "Thing",
    "name": "ACME"
  },
  "aggregateRating": {
    "@type": "AggregateRating",
    "ratingValue": "4.4",
    "reviewCount": "89"
  },
  "offers": {
    "@type": "Offer",
    "priceCurrency": "USD",
    "price": "119.99",
    "priceValidUntil": "2020-11-05",
    "itemCondition": "http://schema.org/UsedCondition",
    "availability": "http://schema.org/InStock",
    "seller": {
      "@type": "Organization",
      "name": "Executive Objects"
    }
  },
  "mydomain:tags" : {}
}
</script>

任何关于我在这里做错的线索将不胜感激。 这可能是一件很愚蠢的事情......

【问题讨论】:

  • 如果您从 json-ld.org/playground 复制产品示例并将其粘贴到 Google 的结构化数据测试工具 (search.google.com/structured-data/testing-tool) 中会出现 4 个错误?
  • 问题中的例子有什么问题?
  • 结构化数据测试工具说它无法识别“产品”类型对象的属性mystuff.com/tags
  • 是的,但您无法更改此设置。 Google 的 SDTT 不是通用的结构化数据验证器,而是 Google 搜索结果功能的验证器,主要使用 Schema.org。
  • 好的,我的例子,带有“mydomain:tags”:{} 是有效的 json-ld?

标签: json-ld web-standards


【解决方案1】:

您的 JSON-LD 似乎是正确的。您正在使用example 19 (Compact IRIs)example 29 (Advanced Context Usage) 的组合。

Google 的结构化数据测试工具不是通用的 JSON-LD 验证器。它报告的错误主要是针对他们的搜索结果功能。他们的错误(“属性 http://mystuff.com/tags 未被 Google 识别为类型为 Product 的对象。”)只是说它不是 Google 知道的属性之一,这当然是正确的。

如果您想验证您的 JSON-LD,而不会出现 Google 特定功能的错误,您可以使用 http://json-ld.org/playground/,例如。

【讨论】:

  • 感谢您的确认。非常感谢。
【解决方案2】:

如果你想在 Django 中为 ListView 和 DetailView 使用 JsonLd,那么你不需要为从管理端添加的所有列表项编写它,你只需要在 List View 类中传递 JsonLdListView 并在 DetailView 中传递 JsonLdDetailView模型中的类和一个函数

第 1 步 在models.py中,在你创建了ListView和DetailView的模型中写下这个函数

@property
    def sd(self):
        return {
            "@type": 'Organization',
            "description": self.description,
            "name": self.name,

        }

*name和description是同一模型的字段名

from django_json_ld.views import JsonLdDetailView, JsonLdListView

第二步

class PortfolioListView(JsonLdListView, ListView):
   pass

第三步

class PortfolioDetailView(JsonLdDetailView, DetailView):
    def get_structured_data(self):
        sd = super(DesignzPortfolioDetailView, 
             self).get_structured_data()

    return sd

       

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-09
    • 2022-11-11
    • 1970-01-01
    • 2017-04-28
    • 2014-04-29
    相关资源
    最近更新 更多