簡體   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