json-ld - 从 JSON-LD 中的前缀和数据构造 IRI
问题描述
在将未编辑的 JSON 数据转换为 JSON-LD 时,使用前缀和数据值为对象构造 IRI 时遇到问题。我运行的示例代码是:
{
"@context" :
{ "prefix" : "http://www.gerastree.at/",
"rdfs" : "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
"@vocab" : "http://example.com/" ,
"load" : "prefix:load"
"items" : "prefix:item"
},
"@type" : "tree",
"@id" : "prefix:t1" ,
"items" :
[
{ "@id" : "prefix:t2",
"@type" : "item",
"load" : "some111"
},
{ "@id" : "prefix:t3",
"@type" : "item",
"load" : "some2222"
}
]
}
但是,当我将@id
值从“prefix:t1”更改为原始 JSON 中的纯数据值(即仅更改为“t1”、“t2”和“t3”)时,不再处理对象。不正确的 JSON-LD 代码(至少不被 阅读riot
)
{
"@context" :
{ "prefix" : "http://www.gerastree.at/",
"rdfs" : "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
"@vocab" : "http://example.com/" ,
"load" : "prefix:load"
"items" : "prefix:item"
},
"@type" : "tree",
"@id" : "t1" ,
"items" :
[
{ "@id" : "t2",
"@type" : "item",
"load" : "some111"
},
{ "@id" : "t3",
"@type" : "item",
"load" : "some2222"
}
]
}
值“t1”等是唯一的,我想将它们与前缀一起用作 IRI,以将数据与其他数据链接起来。有没有一种方法可以在不更改生成 JSON 数据的程序或编辑文件的情况下生成 IRI,并在上下文中添加一些内容。
我找到了一个解决方案(基于Json-LD > Define a "person" for easy use as values on different keys for WebPage schema 的解决方案),但不明白它为什么起作用。
{
"@context" :
{ "prefix" : "http://www.gerastree.at/",
"rdfs" : "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
"@base" : "http://example.com/" ,
"load" : "prefix:load",
"items" : "prefix:item"
},
"@type" : "tree",
"@id" : "t1" ,
"items" :
[
{ "@id" : "t2",
"@type" : "item",
"load" : "some111"
},
{ "@id" : "t3",
"@type" : "item",
"load" : "some2222"
}
]
}
我不认为这是为@type 的@id 或属性的值指定默认URI 的任何方式的重复?.
需要对上下文进行哪些添加和更改?
解决方案
通过对 JSON-LD 建议和实验的更多阅读,我找到了为什么我的第三个版本可以正常工作的解释。
@vocab
应用于属性和对象仅
@base
用于完成主体的 IRI。
不是很明显,但对于我的应用程序来说足够灵活。
推荐阅读
- javascript - 使用键盘输入在平面上移动 Three.js 主体
- php - PHP:无法读取上层目录中的文件
- c# - 如何在我的 wpf 主窗口中更改页面?
- python - 如何根据 Python 日志记录中的严重性格式化日志消息?
- android - 如何解决:无法解析配置的所有依赖项
- javascript - 用于交互式网站的地理方向的谷歌地图 API
- c++ - MPI 在接收特定输入时挂起
- python - 嵌入不会使用 webhook Python 发送到 Discord
- python - Flask:无法在 Python 列表中解压不可迭代的 int 对象
- javascript - 显示 li 包含特定字符串并隐藏不包含的字符串