从JSON-Ld访问Java中的数据

时间:2018-12-18 10:32:52

标签: java rdf jena json-ld

我的服务器正在接收不同的JSON-LD消息作为字符串。我现在需要访问这些消息。这是一个示例:

    {
  "@graph": [
    {
      "@graph": [
        {
          "@id": "InterIoTMsg:meta/b453273f-36d6-49a2-82ec-d909d8e6f10a",
          "@type": [
            "InterIoTMsg:meta",
            "InterIoTMsg:Observation"
          ],
          "InterIoTMsg:ReceiverPlatformId": {
            "@id": "http://om2m.org/myPlatform"
          },
          "InterIoTMsg:conversationID": "conv614d3621-7399-45b1-bf2f-505b24045ea6",
          "InterIoTMsg:dateTimeStamp": "2018-01-15T21:49:00.655+01:00",
          "InterIoTMsg:messageID": "msg7713aa0f-61df-471d-beec-80e8fb71528a"
        }
      ],
      "@id": "InterIoTMsg:metadata"
    },
    {
      "@graph": [
        {
          "@id": "_:b1",
          "@type": "sosa:Observation",
          "iiot:hasName": "tempOutside",
          "sosa:resultTime": {
            "@type": "http://www.w3.org/2001/XMLSchema#dateTime",
            "@value": "2018-04-06T12:36:12Z"
          },
          "sosa:hasResult": {
            "@id": "_:b2"
          },
          "sosa:madeBySensor": {
            "@id": "_:b0"
          }
        },
        {
          "@id": "_:b2",
          "@type": "sosa:Result",
          "iiot:hasResultValue": {
            "@type": "http://www.w3.org/2001/XMLSchema#long",
            "@value": "32"
          },
          "iiot:hasUnit": {
            "@id": "sweet_units:celsius"
          }
        },
        {
          "@id": "http://localhost:8080/weather/station1",
          "@type": "http://inter-iot.eu/GOIoTP#IoTDevice",
          "InterIoT:GOIoTP#hasName": "Weather Station 1"
        },
        {
          "@id": "_:b0",
          "@type": "sosa:Sensor",
          "sosa:isHostedBy": {
            "@id": "http://localhost:8080/in-name/humidity"
          }
        }
      ],
      "@id": "InterIoTMsg:payload"
    }
  ],
  "@context": {
    "InterIoTMsg": "http://inter-iot.eu/message/",
    "InterIoT": "http://inter-iot.eu/",
    "sosa": "http://www.w3.org/ns/sosa/",
    "iiot": "http://inter-iot.eu/GOIoTP#"
  }
}

我不完全知道如何访问数据中的特定值。例如 iiot:hasResultValue 中的 value 。我现在拥有的是RDF解析器,它在耶拿Api的帮助下将String解析为dataset

try (InputStream in = new ByteArrayInputStream(message.getBytes(StandardCharsets.UTF_8))) {
        RDFParser.create()
                .source(in)
                .lang(Lang.JSONLD)
                .parse(dataset.asDatasetGraph());
    }

首先,我不知道该如何继续,其次,我什至不知道这是否合适。因此,如果有人可以帮助我,我将非常感激。

1 个答案:

答案 0 :(得分:0)

从描述中,您可能希望模型中的RDF数据,然后使用模型API访问模型中的数据。

首先尝试将其打印为Turtle,以便您可以看到RDF的结构。

RDFDataMgr.read

RDFParser.create().fromString(message).lang(Lang.JSONLD).parse(model); 也可以通过以下方式完成:

{{1}}