首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将JSON-LD空白节点转换为Apache Jena中的嵌套对象

将JSON-LD空白节点转换为Apache Jena中的嵌套对象
EN

Stack Overflow用户
提问于 2018-05-29 23:31:50
回答 3查看 794关注 0票数 5

我有以下示例Turtle文档:

代码语言:javascript
复制
@prefix dct:   <http://purl.org/dc/terms/> .
@prefix rdf:   <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix example: <http://example.com/vocabulary/> .
@prefix dcat:  <http://www.w3.org/ns/dcat#> .

<http://example.com/datasets/1>
        a                     dcat:Distribution ;
        example:props         [ example:prop1  "hello" ;
                                example:prop2  "1" 
                              ] ;
        dct:description       "test data" .

我使用Apache Jena将其转换为JSON-LD (带有JSONLD_COMPACT_PRETTY的RDFDataMgr)到JSON-LD:

代码语言:javascript
复制
{
  "@context": {
    "dct": "http://purl.org/dc/terms/",
    "rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
    "dcat": "http://www.w3.org/ns/dcat#",
    "example": "http://example.com/vocabulary/"
  },
  "@graph": [
    {
      "@id": "_:b0",
      "example:prop1": "hello",
      "example:prop2": "1"
    },
    {
      "@id": "http://example.com/datasets/1",
      "@type": "dcat:Distribution",
      "example:props": {
        "@id": "_:b0"
      },
      "dct:description": "test data"
    }
  ]
}

但实际上我希望有一个嵌套的对象,而不是一个空白节点:

代码语言:javascript
复制
{
  "@context": {
    "dct": "http://purl.org/dc/terms/",
    "rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
    "dcat": "http://www.w3.org/ns/dcat#",
    "example": "http://example.com/vocabulary/"
  },
  "@graph": [
    {
      "@id": "http://example.com/datasets/1",
      "@type": "dcat:Distribution",
      "example:props": {
         "example:prop1": "hello",
         "example:prop2": "1"
      },
      "dct:description": "test data"
    }
  ]
}

使用Apache Jena可以做到这一点吗?它在语义上是等价的吗?

EN

回答 3

Stack Overflow用户

发布于 2018-05-30 05:20:04

Apache Jena使用jsonld-java进行JSON-LD输入和输出。

可以设置jsonld-java输出,如下所示:

https://jena.apache.org/documentation/io/rdf-output.html#json-ld ==> https://github.com/apache/jena/blob/master/jena-arq/src-examples/arq/examples/riot/Ex_WriteJsonLD.java

你需要咨询jsonld-java,了解作者是否能做你想做的事情。

票数 2
EN

Stack Overflow用户

发布于 2018-05-30 23:17:31

你可以使用带有框架的jsonld-java来转换你的JSON-LD result to nice nested JSON。转换的结果在语义上是等价的。

试一试

代码语言:javascript
复制
    private static String getPrettyJsonLdString(String rdfGraphAsJson) {
        try {
        //@formatter:off
                return JsonUtils
                        .toPrettyString(
                                getFramedJson(
                                        createJsonObject(
                                                        rdfGraphAsJson)));
        //@formatter:on
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


    private static Map<String, Object> getFramedJson(Object json) {
        try {
            return JsonLdProcessor.frame(json, getFrame(), new JsonLdOptions());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    private static Map<String, Object> getFrame() {
        Map<String, Object> frame = new HashMap<>();
        /*
          Use @type to define 'root' object to embed into
        */
        frame.put("@type" , "dcat:Distribution");
        Map<String,Object>context=new HashMap<>();
        context.put("dct", "http://purl.org/dc/terms/");
        context.put("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
        context.put("dcat", "http://www.w3.org/ns/dcat#");
        context.put("example", "http://example.com/vocabulary/");
        frame.put("@context", context);
        return frame;
    }

    private static Object createJsonObject(String ld) {
        try (InputStream inputStream =
                new ByteArrayInputStream(ld.getBytes(Charsets.UTF_8))) {
            Object jsonObject = JsonUtils.fromInputStream(inputStream);
            return jsonObject;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

这将产生

代码语言:javascript
复制
{
  "@context" : {
    "dct" : "http://purl.org/dc/terms/",
    "rdf" : "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
    "dcat" : "http://www.w3.org/ns/dcat#",
    "example" : "http://example.com/vocabulary/"
  },
  "@graph" : [ {
    "@id" : "http://example.com/datasets/1",
    "@type" : "dcat:Distribution",
    "example:props" : {
      "@id" : "_:b0",
      "example:prop1" : "hello",
      "example:prop2" : "1"
    }
  } ]
}
票数 1
EN

Stack Overflow用户

发布于 2020-12-11 19:17:31

您需要将图形re-frame为顶级对象。您可以使用以下任一选项:

代码语言:javascript
复制
{
  "@context": ...,
  "@type": "dcat:Distribution"
}

代码语言:javascript
复制
{
  "@context": ...,
  "@id": "http://example.com/datasets/1"
}

代码语言:javascript
复制
{
  "@context": ...,
  "example:props": {}
}

(即包含任何“example:props”的对象)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50588066

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档