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

如何为匹配已有上下文的对象数组配置正确JSON-LD语法?

解决方案

你不需要修改现有的JSON-LD上下文文件,只需要调整搜索API的响应结构,同时保持使用相同的Link header关联上下文即可。以下是两种符合规范的写法:

1. 直接返回对象数组(简洁写法)

这种方式下,响应是一个JSON数组,数组中的每个元素都遵循你定义的上下文规则,Link header会自动将上下文应用到数组的每一项:

[
  {
    "head": {
      "id": "29a28e6a-6ef9-46bd-8981-f929486821cb",
      "type": "widget",
      "created_by": "theory",
      "created_at": "2024-04-26T01:29:49.542Z",
      "updated_by": "julie",
      "updated_at": "2025-04-23T15:14:51.053Z"
    },
    "body": {
      "username": "Alexandra",
      "active": true,
      "age": 32
    }
  },
  {
    "head": {
      "id": "1b37c4d8-7f1a-4e2b-9c3d-0e1f2a3b4c5d",
      "type": "widget",
      "created_by": "julie",
      "created_at": "2024-05-10T09:15:00.000Z",
      "updated_by": "theory",
      "updated_at": "2025-04-24T10:30:00.000Z"
    },
    "body": {
      "username": "Benjamin",
      "active": false,
      "age": 28
    }
  }
]

2. 使用@graph包裹数组(更明确的JSON-LD图结构)

如果需要更清晰地表明这是一组JSON-LD节点集合,可以返回一个包含@graph属性的对象,@graph的值为目标数组:

{
  "@graph": [
    {
      "head": {
        "id": "29a28e6a-6ef9-46bd-8981-f929486821cb",
        "type": "widget",
        "created_by": "theory",
        "created_at": "2024-04-26T01:29:49.542Z",
        "updated_by": "julie",
        "updated_at": "2025-04-23T15:14:51.053Z"
      },
      "body": {
        "username": "Alexandra",
        "active": true,
        "age": 32
      }
    },
    {
      "head": {
        "id": "1b37c4d8-7f1a-4e2b-9c3d-0e1f2a3b4c5d",
        "type": "widget",
        "created_by": "julie",
        "created_at": "2024-05-10T09:15:00.000Z",
        "updated_by": "theory",
        "updated_at": "2025-04-24T10:30:00.000Z"
      },
      "body": {
        "username": "Benjamin",
        "active": false,
        "age": 28
      }
    }
  ]
}

关键说明

  • Link header保持不变:仍然使用和单个对象API相同的Link header来关联你的上下文,例如:
    Link: <https://your-domain.com/your-context.json>; rel="http://www.w3.org/ns/json-ld#context"; type="application/ld+json"
    
  • 原上下文无需修改:你定义的上下文已经包含了单个对象的所有映射规则,数组中的每个元素会自动应用这些规则,不需要额外调整上下文内容。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.12 16:12:30