簡體   English   中英

如何在 JSON-LD 中引用屬性和值?

[英]How to reference properties and values in JSON-LD?

我找到了一些 JSON-LD 代碼的示例,例如這篇博客文章

{
  "@context": "http://schema.org",
  "@type": "BlogPosting",
  "headline": "14 Ways Json Can Improve Your SEO",
  "alternativeHeadline": "and the women who love them",
  "image": "http://example.com/image.jpg",
  "award": "Best article ever written",
  "editor": "John Doe",
  "genre": "search engine optimization",
  "keywords": "seo sales b2b",
  "wordcount": "1120",
  "publisher": {
    "@type": "Organization",
    "name": "Elsevier",
    "logo": {
      "@type": "ImageObject",
      "url": "http://example.com/logo.jpg"
    }
  },
  "url": "http://www.example.com",
  "datePublished": "2015-09-20",
  "dateCreated": "2015-09-20",
  "dateModified": "2015-09-20",
  "description": "We love to do stuff to help people and stuff",
  "articleBody": "You can paste your entire post in here, and yes it can get really really long.",
  "author": {
    "@type": "Person",
    "name": "Steve"
  },
  "mainEntityOfPage": {
    "@type": "WebPage",
    "@id": "https://example.com/article"
  }
}

在其中,您會看到@context已設置,因此您可以從該上下文中更簡潔地引用名為@typeBlogPosting 然后,您可以直接訪問使用 JSON 中的屬性。

哦,我會注意到,我從未使用過 JSON-LD,只是知道它多年。 在過去的一個小時里,我瀏覽了JSON-LD SpecIRI RFC ,試圖弄清楚如何從其他代碼中引用屬性和值,以及如何組織 JSON-LD。

在 JSON-LD 中, #標簽的詳細含義是什么?

例如,他們有這個:

{
  "@context": {
    "label": "http://www.w3.org/2000/01/rdf-schema#label"
  },
  "@id": "",
  "label": "Just a simple document"
}

當我訪問 url http://www.w3.org/2000/01/rdf-schema時,我得到了一些原始 RDF 文本。 我將label視為 RDF 中的屬性定義,但它是上下文 label http://www.w3.org/2000/01/rdf-schema#label我們的 JSON-LD 代碼? 對我來說,計算機似乎無法:

  1. http://www.w3.org/2000/01/rdf-schema獲取內容。
  2. 意識到它是 RDF。
  3. 解析 RDF。
  4. 深入了解label屬性。
  5. 並使用它來了解如何在我們的 JSON-LD 中鍵入 label 屬性。

另一個例子在這里,使用http://schema.org

{
  "@context": {
    "name": "http://schema.org/name",   
    "image": {
      "@id": "http://schema.org/image",   
      "@type": "@id"   
    },
    "homepage": {
      "@id": "http://schema.org/url",   
      "@type": "@id"   
    }
  }
}

當我訪問http://schema.org/url時,它是一個 HTML 頁面(不是 RDF 或 JSON),它顯示如下。

在此處輸入圖像描述


They don't appear to have a http://schema.org/url.json or .rdf URL, so again it appears this "mental association" between http://schema.org/url and an actual URL property isn' t 由計算機處理或計算出來的東西,但只是一種粗略的心理聯想。 Basically, we added the http://schema.org/url HTML webpage so if you happen to visit http://schema.org/url it shows something, and because it's nice to have some web docs:). 但是沒有技術需要有一個網站或 JSON 來下載定義或諸如此類的東西。 最后,像http://schema.org/url這樣的鏈接只是用作標識符的字符串。 我這樣做對嗎?

那么這將意味着像http://www.w3.org/2000/01/rdf-schema#label或其他的#標簽只是另一個字符串,但是編譯 JSON-LD 的代碼會從標簽后部分分離出來並使用它在 JSON 中查找和鍵入屬性。 但是,它沒有任何實際意義,因為它托管在該域/URL 下的網頁上。

假設我有一堆要創建的 URL:

  • /person/john-kennedy
  • /atom/hydrogen
  • 等等

每個都有一堆可能深度嵌套的屬性/對象/值。 所以我可以這樣做:

{
  "@context": "https://mywebsite.org",
  "@type": "atom",
  "slug": "hydrogen",
  "name": "Hydrogen",
  "bindsTo": [{ "@id": "https://mywebsite.org/atom/oxygen" }]
}

或者更好的是,以某種方式做這樣的事情:

{
  "@context": "https://mywebsite.org",
  "@type": "atom",
  "@id": "/atom/hydrogen",
  "name": "Hydrogen",
  "bindsTo": [{ "@id": "/atom/oxygen" }]
}

簡而言之,我可以做些什么來使用 JSON-LD 以盡可能干凈的方式鏈接數據(最少的文本)?

  • 是否需要有一個實際的網絡存在來支持實際的結構化 JSON 數據,或者它只是不合常規或創造某種友好的用戶體驗,但機器不需要?
  • 標簽只是進入屬性的約定嗎? 是否可以使用主題標簽深入挖掘屬性(或數組項),例如#foo/bar[1]/baz可以深入挖掘{ foo: { bar: [ { baz: 1 }, { baz: 2 } ] } }返回 2? 還是托管網站實際上以某種方式使用了主題標簽?

在解析術語 URI 時提供詞匯表/術語的 RDF 定義是一種很好的做法,但這不是必需的。 尤其是因為任何類型的 URI 都可以用於 RDF 術語,而不僅僅是可解析的 URI(例如, urntag而不是http )。

有幾種方法可以以機器可讀的方式提供詞匯定義。 W3C 工作組注釋發布 RDF 詞匯表的最佳實踐食譜記錄了其中的一些。

附錄 B解釋了# (與/ )在 RDF 術語 URI 中的作用。 這只是為詞匯表構建 URI 命名空間的一種方式(也可以用來區分術語/事物和描述術語/事物的文檔)。

是的,RDF 術語 URI 的基本作用是它們充當全局唯一標識符。


與其他 RDF 語法相比,JSON-LD 的特殊之處在於可以加載遠程 JSON-LD 上下文。 例如,在 Schema.org 的情況下,當訪問首頁(使用alternate鏈接類型)時,它們的 JSON-LD 上下文鏈接在鏈接 HTTP header 中。 這樣的上下文只允許編寫更短的 JSON-LD。

在了解關聯數據背景時,此功能和其他 JSON-LD 功能可能會令人困惑。 三重形式“思考”數據可能會有所幫助。 流行的 RDF 語法Turtle接近這個概念。 如果您知道三元組應該是什么樣子,那么只需使用 JSON-LD 的語法特性來具體化您想說的話。 在許多情況下,編寫/生成 Turtle 然后自動將其轉換為 JSON-LD(或任何其他 RDF 語法)更容易。

JSON-LD

# on https://mywebsite.example.com/
{
  "@context": "https://vocabulary.example.org",
  "@type": "Atom",
  "@id": "/atom/hydrogen",
  "name": "Hydrogen",
  "bindsTo": [{ "@id": "/atom/oxygen" }]
}

在 Turtle 中可以這樣表示:

@prefix : <https://vocabulary.example.org/> .

<https://mywebsite.example.com/atom/hydrogen>
  a        :Atom ;
  :bindsTo <https://mywebsite.example.com/atom/oxygen> ;
  :name    "Hydrogen" .
@prefix : <https://vocabulary.example.org/> .

<https://mywebsite.example.com/atom/hydrogen> a        :Atom .
<https://mywebsite.example.com/atom/hydrogen> :bindsTo <https://mywebsite.example.com/atom/oxygen> .
<https://mywebsite.example.com/atom/hydrogen> :name    "Hydrogen" .
<https://mywebsite.example.com/atom/hydrogen> a                                        <https://vocabulary.example.org/Atom> .
<https://mywebsite.example.com/atom/hydrogen> <https://vocabulary.example.org/bindsTo> <https://mywebsite.example.com/atom/oxygen> .
<https://mywebsite.example.com/atom/hydrogen> <https://vocabulary.example.org/name>    "Hydrogen" .
<https://mywebsite.example.com/atom/hydrogen> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <https://vocabulary.example.org/Atom> .
<https://mywebsite.example.com/atom/hydrogen> <https://vocabulary.example.org/bindsTo>          <https://mywebsite.example.com/atom/oxygen> .
<https://mywebsite.example.com/atom/hydrogen> <https://vocabulary.example.org/name>             "Hydrogen" .

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM