繁体   English   中英

从JSON-Ld访问Java中的数据

[英]Accessing data in java from 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中的 我现在拥有的是RDF-Parser,它在Jena Api的帮助下将String解析为dataset

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

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

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

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

Model model = ModelFactory.createDefaultModel();
RDFDataMgr.read(model, new StringReader(message), null, Lang.JSONLD);
RDFDataMgr.write(System.out, model, Lanmg.TURTLE);

RDFDataMgr.read也可以通过以下方式完成:

RDFParser.create().fromString(message).lang(Lang.JSONLD).parse(model);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM