ijd8.COM

A simple blog for an old Ma Nong.

使用 Go 语言生成 Json-LD 数据结构

Permalink

一个页面的 Json-LD 数据结构不仅对搜索显示很友好,对用户以富媒体方式展示,还对搜索引擎很友好。这里用 Go 语言来实现网页 Article 类型的 Json-LD 结构。

可以参考本站文章页面 head 里面的 ld+json 内容。

JSON: ld+json
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
{
	"@context": "http://schema.org/",
	"@graph": [{
		"@type": "Organization",
		"logo": "https://ijd8.com/static/logo_112.png",
		"url": "https://ijd8.com"
	}, {
		"@type": "BreadcrumbList",
		"itemListElement": [{
			"@type": "ListItem",
			"position": 1,
			"name": "ijd8.COM",
			"item": "https://ijd8.com"
		}, {
			"@type": "ListItem",
			"position": 2,
			"name": "Python",
			"item": "https://ijd8.com/n/4"
		}]
	}, {
		"@type": "Article",
		"dateModified": "2021-01-28T16:32:37+08:00",
		"datePublished": "2014-12-30T18:53:25+08:00",
		"headline": "让Tornado 使用 ujson",
		"image": ["https://ijd8.com/static/upload/fc96c68e56ab8ad09a8b66fbe05135d4.jpg"],
		"author": {
			"@type": "Person",
			"name": "简单猪"
		},
		"publisher": {
			"@type": "Organization",
			"name": "ijd8.COM",
			"logo": {
				"@type": "ImageObject",
				"url": "https://ijd8.com/static/logo_112.png"
			}
		},
		"description": "Tornado 默认使用json,但是它太慢了,我想用ujson 来代替。",
		"mainEntityOfPage": "https://ijd8.com/t/37",
		"speakable": {
			"@type": "SpeakableSpecification",
			"xpath": ["/html/head/title", "/html/head/meta[@name='description']/@content"]
		}
	}]
}

注意问题

  • 不要滥用,不要作弊;
  • 选定结构化数据后,一定要填充所有属性,否则被认为不适合以富媒体的方式展现;
  • 不要在列表页上使用结构化数据(轮播Carousel除外),要用在详情页上;
  • 两个内容相同的网页,它们的结构化数据也要完全一致;
  • 一个网页上可以添加多个结构化数据,确保结构化数据索引的内容都是用户可见的;
  • 结构化数据中指定的图片必须要位于当前网页上;
  • 图片的 URL 必须允许搜索引擎抓取和索引;
  • 列表类型的页面上要么标记所有的对象,要么都不标记,除了轮播Carousel外,结构化数据不得链接到详情页。

生成结构体

使用 json-to-struct 工具可以自动生成一个 struct 结构:

Go: Json-LD-struct
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
type AutoGenerated struct {
	Context string `json:"@context"`
	Graph   []struct {
		Type            string `json:"@type"`
		Logo            string `json:"logo,omitempty"`
		URL             string `json:"url,omitempty"`
		ItemListElement []struct {
			Type     string `json:"@type"`
			Position int    `json:"position"`
			Name     string `json:"name"`
			Item     string `json:"item"`
		} `json:"itemListElement,omitempty"`
		DateModified  time.Time `json:"dateModified,omitempty"`
		DatePublished time.Time `json:"datePublished,omitempty"`
		Headline      string    `json:"headline,omitempty"`
		Image         []string  `json:"image,omitempty"`
		Author        struct {
			Type string `json:"@type"`
			Name string `json:"name"`
		} `json:"author,omitempty"`
		Publisher struct {
			Type string `json:"@type"`
			Name string `json:"name"`
			Logo struct {
				Type string `json:"@type"`
				URL  string `json:"url"`
			} `json:"logo"`
		} `json:"publisher,omitempty"`
		Description      string `json:"description,omitempty"`
		MainEntityOfPage string `json:"mainEntityOfPage,omitempty"`
		Speakable        struct {
			Type  string   `json:"@type"`
			Xpath []string `json:"xpath"`
		} `json:"speakable,omitempty"`
	} `json:"@graph"`
}

工具虽然方便,但还是建议手动分开为小的结构体,否则会生成很多值为 null 的键。

下面是分开的示例:

Go: Json-LD 数据结构
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
type (
	JsonLd struct {
		Context string        `json:"@context"`
		Graph   []interface{} `json:"@graph"`
	}

	JsOrganization struct {
		Type string `json:"@type"`
		Logo string `json:"logo"`
		URL  string `json:"url"`
	}

	JsItemListElement struct {
		Type     string `json:"@type"`
		Position int    `json:"position"`
		Name     string `json:"name"`
		Item     string `json:"item"`
	}
	JsBreadcrumbList struct {
		Type            string              `json:"@type"`
		ItemListElement []JsItemListElement `json:"itemListElement"`
	}

	JsAuthor struct {
		Type string `json:"@type"`
		Name string `json:"name"`
	}

	JsLogo struct {
		Type string `json:"@type"`
		URL  string `json:"url"`
	}

	JsPublisher struct {
		Type string `json:"@type"`
		Name string `json:"name"`
		Logo JsLogo `json:"logo"`
	}

	JsSpeakable struct {
		Type  string   `json:"@type"`
		Xpath []string `json:"xpath"`
	}

	JsArticle struct {
		Type             string      `json:"@type"`
		DateModified     string      `json:"dateModified"`
		DatePublished    string      `json:"datePublished"`
		Headline         string      `json:"headline"`
		Image            []string    `json:"image"`
		Author           JsAuthor    `json:"author"`
		Publisher        JsPublisher `json:"publisher"`
		Description      string      `json:"description"`
		MainEntityOfPage string      `json:"mainEntityOfPage"`
		Speakable        JsSpeakable `json:"speakable"`
	}
)

使用起来就很方便。

结构测试网址 https://search.google.com/test/rich-results?utm_campaign=sdtt&utm_medium=code

Write a Comment

Submit Comment Login
Based on Golang + fastHTTP + sdb | go1.16.7 Processed in 1ms