You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用JSON-LD Frame扩展实例@type关联的类属性?

用JSON-LD Frame扩展实例类属性关联的解决方案

需求说明

需要将实例@type数组中的类,关联并展开其对应的属性信息。

原始JSON-LD文档

{
  "@graph": [
    {
      "@id": "ex:Class1",
      "view:view": {
        "@id": "view:Class1View"
      },
      "view:preferredView": {
        "@id": "view:Class1View"
      }
    },
    {
      "@id": "exData:Class1-Instance1",
      "@type": [
        "ex:Class1",
        "ex:class2"
      ]
    }
  ],
  "@context": {
    "ex": "https://example.com/ontologies/ex#",
    "view": "http://www.example.com/ontologies/view#",
    "exData": "https://example.com/data/ex#"
  }
}

期望输出(注:该格式不符合JSON-LD规范)

{
  "@context": {
    "ex": "https://example.com/ontologies/ex#",
    "view": "http://www.example.com/ontologies/view#",
    "exData": "https://example.com/data/ex#"
  },
  "@id": "exData:Class1-Instance1",
  "@type": [
    "ex:Class1": {
        "view:view": [{
            "@id": "views:Class1View"
        }],
        "view:preferredView": {
            "@id": "views:Class1View"
        }
    },
    "ex:class2": {}
  ]
}

尝试过的Frame配置(未成功)

{
  "@context": {
    "ex": "https://example.com/ontologies/ex#",
    "view": "http://www.example.com/ontologies/view#",
    "exData": "https://example.com/data/ex#"
  },
  "@id": "exData:Class1-Instance1",
  "@type": [{
      "view:view": [{
           
      }],
      "view:preferredView": {}
  }]
}

正确解决方案

首先明确:你期望的输出格式不符合JSON-LD规范,因为@type的合法值只能是IRI、字符串或包含@id的对象,不能是键值对结构。因此我们换一种合规方式实现类似效果:新增自定义字段承载类属性信息,同时保留标准@type字段。

合规的Frame配置

{
  "@context": {
    "ex": "https://example.com/ontologies/ex#",
    "view": "http://www.example.com/ontologies/view#",
    "exData": "https://example.com/data/ex#",
    "typeDetails": "https://example.com/terms/typeDetails"
  },
  "@id": "exData:Class1-Instance1",
  "@type": {},
  "typeDetails": {
    "@embed": "@always",
    "@explicit": false,
    "@requireAll": false,
    "@type": {
      "view:view": {},
      "view:preferredView": {}
    }
  }
}

配置说明

  • @id: 定位到目标实例exData:Class1-Instance1
  • @type: 保留实例的原始类型数组
  • typeDetails: 自定义字段,用于嵌入每个类型对应的类属性
    • @embed: @always: 强制嵌入关联的类节点,不保留引用
    • @explicit: false: 包含所有匹配的属性,不管是否在Frame中显式声明
    • 内部@type结构:指定需要展开的类属性

处理后的合规输出

{
  "@context": {
    "ex": "https://example.com/ontologies/ex#",
    "view": "http://www.example.com/ontologies/view#",
    "exData": "https://example.com/data/ex#",
    "typeDetails": "https://example.com/terms/typeDetails"
  },
  "@id": "exData:Class1-Instance1",
  "@type": ["ex:Class1", "ex:class2"],
  "typeDetails": [
    {
      "@id": "ex:Class1",
      "view:view": {"@id": "view:Class1View"},
      "view:preferredView": {"@id": "view:Class1View"}
    },
    {
      "@id": "ex:class2"
    }
  ]
}

接近期望格式的额外处理

如果一定要得到最初期望的非规范格式,需要在Frame处理完成后手动进行JSON转换:将typeDetails中的内容映射到@type数组的键值对结构中。这一步不属于JSON-LD Frame的标准功能,需自行编写代码实现。

内容的提问来源于stack exchange,提问作者agustaf

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 19:17:39