JSON-LD 教程
简介
JSON-LD(JSON for Linking Data)是一种轻量级的数据格式,旨在在 Web 上简化结构化数据的使用。它基于已广泛使用的 JSON 格式,并扩展了对链接数据概念的支持,使其成为语义网和数据互联互通的重要技术。
JSON-LD 的主要优势:
- 基于 JSON,对开发者友好且易于阅读
- 提供了数据的语义上下文
- 支持数据互操作性
- 易于在现有系统中集成
- 被搜索引擎广泛支持,有助于 SEO
基本概念
在深入学习 JSON-LD 之前,需要理解几个关键概念:
- 主题(Subject):我们要描述的事物
- 谓词(Predicate):事物的属性或与其他事物的关系
- 对象(Object):属性的值或关联的另一个事物
- IRI(国际化资源标识符):用于唯一标识资源的字符串
这些概念构成了 RDF(资源描述框架)的基础,JSON-LD 本质上是 RDF 数据模型的 JSON 表示。
JSON-LD 语法
JSON-LD 文档是一个合法的 JSON 文档,通常包含以下关键字:
@context
:定义属性与 IRI 的映射关系@type
:指定实体的类型@id
:为实体提供唯一标识符@value
:指定属性的字面值@language
:指定字面值的语言@list
:表示有序集合@set
:表示无序集合@graph
:定义命名图
下面是一个简单的 JSON-LD 示例:
{
"@context": "https://schema.org/",
"@type": "Person",
"@id": "https://example.com/person/123",
"name": "张三",
"jobTitle": "软件工程师",
"telephone": "+86-123-4567-8900",
"url": "https://example.com/zhangsan"
}
上下文(Context)
上下文(@context
)是 JSON-LD 最重要的特性之一,它为数据提供语义。上下文可以:
- 将简短的属性名映射到完整的 IRI
- 指定默认的词汇表
- 定义数据类型
- 建立语言映射
内联上下文
{
"@context": {
"name": "https://schema.org/name",
"jobTitle": "https://schema.org/jobTitle",
"telephone": "https://schema.org/telephone",
"url": "https://schema.org/url"
},
"@type": "Person",
"name": "张三",
"jobTitle": "软件工程师",
"telephone": "+86-123-4567-8900",
"url": "https://example.com/zhangsan"
}
外部上下文
{
"@context": "https://schema.org/",
"@type": "Person",
"name": "张三",
"jobTitle": "软件工程师",
"telephone": "+86-123-4567-8900",
"url": "https://example.com/zhangsan"
}
多上下文
{
"@context": [
"https://schema.org/",
{
"custom": "https://example.org/custom/"
}
],
"@type": "Person",
"name": "张三",
"jobTitle": "软件工程师",
"custom:skill": "JavaScript"
}
类型(Type)
@type
关键字用于指定实体的类型,帮助处理程序理解数据代表什么。
{
"@context": "https://schema.org/",
"@type": "Person",
"name": "张三"
}
多类型示例:
{
"@context": "https://schema.org/",
"@type": ["Person", "Organization"],
"name": "张三公司"
}
标识符(ID)
@id
为实体提供全局唯一标识符,通常是 URL:
{
"@context": "https://schema.org/",
"@type": "Person",
"@id": "https://example.com/person/123",
"name": "张三"
}
使用 ID 建立关系
{
"@context": "https://schema.org/",
"@type": "Person",
"@id": "https://example.com/person/123",
"name": "张三",
"worksFor": {
"@id": "https://example.com/company/456",
"@type": "Organization",
"name": "示例公司"
}
}
嵌套属性
JSON-LD 支持嵌套属性,允许表示复杂的数据结构:
{
"@context": "https://schema.org/",
"@type": "Person",
"name": "张三",
"address": {
"@type": "PostalAddress",
"streetAddress": "北京市海淀区中关村大街1号",
"addressLocality": "北京",
"postalCode": "100080",
"addressCountry": "中国"
}
}
数组和列表
无序集合(默认)
{
"@context": "https://schema.org/",
"@type": "Person",
"name": "张三",
"jobTitle": ["软件工程师", "项目经理"]
}
有序列表
{
"@context": "https://schema.org/",
"@type": "HowTo",
"name": "如何制作蛋糕",
"step": {
"@list": [
{
"@type": "HowToStep",
"name": "准备材料",
"text": "准备面粉、鸡蛋、牛奶和糖"
},
{
"@type": "HowToStep",
"name": "混合材料",
"text": "将所有材料混合均匀"
},
{
"@type": "HowToStep",
"name": "烘焙",
"text": "放入预热好的烤箱,180度烘焙30分钟"
}
]
}
}
命名图(Named Graphs)
命名图允许在单个文档中包含多个相关但独立的数据集:
{
"@context": "https://schema.org/",
"@graph": [
{
"@type": "Person",
"@id": "https://example.com/person/123",
"name": "张三",
"worksFor": {"@id": "https://example.com/company/456"}
},
{
"@type": "Organization",
"@id": "https://example.com/company/456",
"name": "示例公司",
"employee": {"@id": "https://example.com/person/123"}
}
]
}
JSON-LD 框架
JSON-LD 框架是一种模板,可用于转换 JSON-LD 文档:
{
"@context": "https://schema.org/",
"@type": "Person",
"name": {},
"jobTitle": {}
}
应用此框架可提取特定属性或改变文档结构。
实际应用场景
1. 搜索引擎优化(SEO)
<script type="application/ld+json">
{
"@context": "https://schema.org/",
"@type": "Product",
"name": "高级智能手机",
"image": "https://example.com/phone.jpg",
"description": "最新款高性能智能手机,配备先进的相机系统",
"brand": {
"@type": "Brand",
"name": "科技品牌"
},
"offers": {
"@type": "Offer",
"price": "4999.00",
"priceCurrency": "CNY",
"availability": "https://schema.org/InStock"
}
}
</script>
2. API 响应
{
"@context": "https://example.com/api/context",
"@type": "SearchResults",
"query": "智能手机",
"totalResults": 243,
"itemListElement": [
{
"@type": "Product",
"name": "高级智能手机",
"price": "4999.00"
},
{
"@type": "Product",
"name": "中端智能手机",
"price": "2999.00"
}
]
}
3. 知识图谱
{
"@context": "https://schema.org/",
"@graph": [
{
"@type": "Person",
"@id": "https://example.com/people/zhang-san",
"name": "张三",
"knows": {"@id": "https://example.com/people/li-si"}
},
{
"@type": "Person",
"@id": "https://example.com/people/li-si",
"name": "李四",
"knows": {"@id": "https://example.com/people/zhang-san"}
}
]
}
工具和库
1. JSON-LD 处理库
- JavaScript: jsonld.js
- Python: PyLD
- Java: jsonld-java
- PHP: php-json-ld
- Ruby: json-ld
2. 在线工具
- JSON-LD Playground: https://json-ld.org/playground/
- Schema.org Validator: https://validator.schema.org/
- Google 结构化数据测试工具: https://search.google.com/test/rich-results
常见问题解答
JSON-LD 与普通 JSON 有何不同?
JSON-LD 是 JSON 的超集,保持与 JSON 的向后兼容性,但添加了特殊的关键字(如 @context
、@type
、@id
)来提供语义上下文。
为什么要使用 JSON-LD 而不是普通 JSON?
JSON-LD 提供了结构化数据的语义上下文,使数据更易于被机器理解和处理,同时保持了 JSON 的简洁性和可读性。它尤其适用于需要数据互操作性和语义明确性的场景。
如何将现有 JSON 转换为 JSON-LD?
- 添加
@context
定义属性的语义 - 添加
@type
指定实体类型 - 使用
@id
为实体提供唯一标识符 - 根据需要调整嵌套结构和属性名
JSON-LD 与 RDF 的关系是什么?
JSON-LD 是 RDF 数据模型的一种序列化格式,与 RDFa、Turtle 等格式并列。它提供了一种以 JSON 表示 RDF 三元组的方法,使数据既符合语义网标准,又便于在 Web 应用中使用。