简体   繁体   English

从JSON-Ld访问Java中的数据

[英]Accessing data in java from JSON-Ld

my server is receiving different JSON-LD messages as a string. 我的服务器正在以字符串形式接收不同的JSON-LD消息。 I now need to acces these messages. 我现在需要访问这些消息。 Here is an example: 这是一个例子:

    {
  "@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#"
  }
}

I don't know exactly how to access specific values in the data. 我不确切地知道如何访问数据中的特定值。 For example value in iiot:hasResultValue . 例如iiot:hasResultValue中的 What I have right now is a RDF-Parser which parses the String to a dataset with the help of the Jena Api: 我现在拥有的是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());
    }

First of all I don't know how to continue at this point, and second I don't even know if this is the right rudiment. 首先,我现在不知道如何继续,其次,我什至不知道这是否合适。 So if anybody could help I am very grateful. 因此,如果有人可以帮助我,我非常感激。

From the description, you probably want the RDF data in a model then use the model API to access the data in the model. 从描述中,您可能想要模型中的RDF数据,然后使用模型API访问模型中的数据。

The first thing to try is print as Turtle so you can see the structure of the RDF. 首先尝试将其打印为Turtle,以便您可以看到RDF的结构。

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

The RDFDataMgr.read can also be done with: 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