[英]Define JSON-LD @context to join/split values?
我想使用jsonld.js库的expand
和compact
方法将来自各种源的数据转换为通用格式进行处理。 如果我获取源JSON文档,向它添加一个@context
,然后通过expand
方法传递它我能够获得我需要的通用格式。
我无法找到解决方案的用例是需要合并多个值时。 例如,schema.org限定PostalAddress
与用于单个字段streetAddress
,但许多系统存储所述街道地址作为单独的值(街道号,街道名称,街道方向...)。 要将传入的数据转换为schema.org格式,我需要一种方法在我的@context
中指示多个字段以正确的顺序组成streetAddress
。
压缩文件
{
"@context": {
"displaName": "http://schema.org/name",
"website": "http://schema.org/homepage",
"icon": "http://schema.org/image",
"streetNumber": "http://schema.org/streetAddress"
},
"displaName": "John Doe",
"website": "http://example.com/",
"icon": "http://example.com/images/test.png",
"streetNumber": "123",
"streetName": "Main St",
"streetDirection": "South"
}
扩展文件
{
"http://schema.org/name":[
{
"@value":"John Doe"
}
],
"http://schema.org/image":[
{
"@value":"http://example.com/images/test.png"
}
],
"http://schema.org/streetAddress":[
{
"@value":"123"
}
],
"http://schema.org/homepage":[
{
"@value":"http://example.com/"
}
]
}
我已经回顾了我能找到的所有JSON-LD规范,但是找不到任何指示使用@context
分割或连接值的方法的东西。
是否有人知道一种方法,以正确的顺序将多个值映射到一个上下文属性,并可能在值之间添加空格。 我还需要找到一个反向场景的解决方案,我需要按正确的顺序将一个字段拆分成多个值。
注意:即使我将所有三个属性映射到streetAddress
,这些值都将包含在数组中,但不能保证它们的顺序正确。
实现此目的的一种可能方法是对包含有序地址组件的地址使用单个数组字段(即["number", "direction", "name"]
)。 然后在@context
您可以使用@container: @list
指定address
,这将确保正确排序地址组件。
所以压缩文件将是:
{
"@context": {
"displaName": "http://schema.org/name",
"website": "http://schema.org/homepage",
"icon": "http://schema.org/image",
"address": {
"@id": "http://schema.org/streetAddress",
"@container": "@list"
}
},
"displaName": "John Doe",
"website": "http://example.com/",
"icon": "http://example.com/images/test.png",
"address": ["123", "South", "Main St"]
}
而扩展的那个将是
{
"http://schema.org/streetAddress": [
{
"@list": [
{
"@value": "123"
},
{
"@value": "South"
},
{
"@value": "Main St"
}
]
}
],
"http://schema.org/name": [
{
"@value": "John Doe"
}
],
"http://schema.org/image": [
{
"@value": "http://example.com/images/test.png"
}
],
"http://schema.org/homepage": [
{
"@value": "http://example.com/"
}
]
}
我在jsonld.js Github存储库上发布了一个问题。 根据jsonld.js库的原始创建者@dlongley,使用标准JSON-LD无法操纵此庄园中的属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.