如何操纵Json-ld?

How to manipulate Json-ld?

我有这个 json-ld:

{ "@id":   "http://www.example.com/john-doe",
    "@type":   "http://xmlns.com/foaf/0.1/Person",
    "http://xmlns.com/foaf/0.1/name":   "John   Doe",
    "http://xmlns.com/foaf/0.1/age":   {
    "@value":   "42",
    "@kind":   "http://www.w3.org/2001/XMLSchema#nonNegativeInteger"},
    "http://xmlns.com/foaf/0.1/knows" : [
      {   "@id":   "http://www.example.com/charlie-brown"   },
      {   "@id":   "http://www.example.com/jane-doe"   }
    ]
  }

我必须写下上下文,以便下面的 json-ld 有效。

{ "@context" : "context to write....",
  "@id":   "john-doe",
  "@type":   "person",
  "name":   "John   Doe",
  "age":   "42",
  "knows":   ["charlie-brown", "jane-doe"]
}

我写了一个解决方案,但是不完整,我不知道如何写一个完整的解决方案,希望有人能帮助我。

您必须使用 framing 才能获得接近您要求的结果。

  • 请参阅此答案以获取 Java 完整代码示例,
  • 另请参阅此答案,其中包含一些额外提示

在 json-ld 操场上使用这个 link for an example。这就是嵌入了@context 的文档的样子:

{
  "@context": {
    "name": {
      "@id": "http://xmlns.com/foaf/0.1/name"
    },
    "age": {
      "@id": "http://xmlns.com/foaf/0.1/age"
    },
    "knows": {
      "@id": "http://xmlns.com/foaf/0.1/knows",
      "@container": "@set"
    }
  },
 "@id": "john-doe",
 "@type": "person",
  "name": "John   Doe",
  "age": "42",
  "knows": [
    "charlie-brown",
    "jane-doe"
  ]
}

这是框架的样子

{
  "@context": {
    "name": {
      "@id": "http://xmlns.com/foaf/0.1/name"
    },
    "age": {
      "@id": "http://xmlns.com/foaf/0.1/age"
    },
    "knows": {
      "@id": "http://xmlns.com/foaf/0.1/knows",
      "@container": "@set"
    }
  }
}

这将渲染到

{
  "@context": {
    "name": {
      "@id": "http://xmlns.com/foaf/0.1/name"
    },
    "age": {
      "@id": "http://xmlns.com/foaf/0.1/age"
    },
     "knows": {
      "@id": "http://xmlns.com/foaf/0.1/knows",
      "@container": "@set"
    }
  },
  "@graph": [
    {
      "@id": "john-doe",
      "@type": "https://json-ld.org/playground/person",
      "age": "42",
      "knows": [
        "charlie-brown",
        "jane-doe"
      ],
      "name": "John   Doe"
    }
  ]
}