繁体   English   中英

有没有办法让耶拿将Json-ld读入模型?

[英]Is there a way to get jena to read Json-ld into a model?

我在这里使用的很多东西都不是很有经验。 这是针对学校项目的,我们将在该项目中构建使用语义结构化数据的软件。

我从Frost api( https://frost.met.no/ )以json-ld格式获取气象数据。 我想将其读入耶拿模型。 我对耶拿是否支持这一点感到有些困惑。

import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.JsonNode;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.exceptions.UnirestException;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.ModelFactory;

import java.io.ByteArrayInputStream;
import java.io.InputStream;


public class Main {
    public static void main(String[] args) {
        //authentication for the frost api. I have a feeling i shouldn't share this online
        String auth = "######";

        // I use unirest to make the request to the api
        HttpResponse<JsonNode> response = null;
        try {
            response = Unirest.get("https://frost.met.no/sources/v0.jsonld?country=Norge").
                    basicAuth(auth, "").
                    asJson();
        } catch (UnirestException e) {
            e.printStackTrace();
        }

        // Get the data as string. Remove context tag as it gives an error:
        // "org.apache.jena.riot.RiotException: loading remote context failed: https://frost.met.no/schema"
        // I'm assuming this is a problem on the api side. If anyone has any insights feel free to share
        String jsonString = response.getBody().toString();
        jsonString = jsonString.replace("\"@context\":\"https://frost.met.no/schema\",", "");

        // Print the json-ld string. Looks like it should.
        System.out.println(jsonString);

        //convert json-ld string into InputStream as is required by the read() function.
        InputStream targetStream = new ByteArrayInputStream(jsonString.getBytes());
        Model model = ModelFactory.createDefaultModel() ;

        try {
            model.read(targetStream, "", "JSON-LD") ;
        } catch (final Exception e){
            System.out.println(e.toString());
        }

        // Write model to console. This seems to output an empty model
        model.write(System.out, "JSON-LD");

    }
}

我得到的响应看起来像这样:

{
    "@context": "https://frost.met.no/schema",
    "@type": "SourceResponse",
    "apiVersion": "v0",
    "license": "https://creativecommons.org/licenses/by/3.0/no/",
    "createdAt": "2019-03-27T14:00:46Z",
    "queryTime": 0.534,
    "currentItemCount": 1685,
    "itemsPerPage": 1685,
    "offset": 0,
    "totalItemCount": 1685,
    "currentLink": "https://frost.met.no//auth//sources/v0.jsonld?country=Norge",
    "data": [
        {
            "@type": "SensorSystem",
            "id": "SN100",
            "name": "PLASSEN",
            "shortName": "Plassen",
            "country": "Norge",
            "countryCode": "NO",
            "geometry": {
                "@type": "Point",
                "coordinates": [
                    12.5039,
                    61.1349
                ],
                "nearest": false
            },

还有很多,但是关于不同的SensorSystems的数据更多。

我没有收到任何错误,但是它输出的模型似乎是空的:

{
  "@id" : "_:b0",
  "@type" : "file:///C:/Users/bm_93/Desktop/Fag/INFO216/SemesterOppgave/SourceResponse"
}

我做对了吗? 耶拿支持吗?

如果没有,我可以做些什么来将此json数据转换为jena模型?

Jena支持JSON-LD读写。

我们无法查看您要读取的输入内容,因为它位于登录名后面,但通常,如果存在@context链接,则解析器需要检索该上下文,否则将无法获取JSON-LD正确阅读。

上下文的URL是https://frost.met.no/schema ,但是据我所知,这是网页的URL,并且没有发布JSON-LD上下文。 因此,Frost API似乎有问题。

您始终可以将响应视为普通JSON ...

暂无
暂无

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

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