Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 有没有办法让jena将Json ld读入模型?_Java_Jena - Fatal编程技术网

Java 有没有办法让jena将Json ld读入模型?

Java 有没有办法让jena将Json ld读入模型?,java,jena,Java,Jena,我在这里使用的很多东西都不是很有经验。这是一个学校项目,我们将构建使用语义结构数据的软件 我正在从Frost api()获取气象数据 json ld格式。我想把这个读入耶拿模型。我有点困惑耶拿是否支持这一点 import com.mashape.unirest.http.HttpResponse; import com.mashape.unirest.http.JsonNode; import com.mashape.unirest.http.Unirest; import com.mashap

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

我正在从Frost api()获取气象数据 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");

    }
}
还有很多,但只是关于不同传感器系统的更多数据

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

{
  "@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…

所以它不起作用的最可能原因是它缺少@context?如果我将其视为常规JSON,有没有一种方法可以在不编写手动解析器的情况下使用它创建jena模型?我编辑了我的原始帖子,展示了一些回复的样子。这似乎是最可能的原因。您可以尝试编写自己的上下文来定义JSON键如何映射到RDF属性,但首先要尝试的是联系运行该API的人员并询问他们如何获取上下文文件,因为它似乎不在响应所说的URL处:
{
  "@id" : "_:b0",
  "@type" : "file:///C:/Users/bm_93/Desktop/Fag/INFO216/SemesterOppgave/SourceResponse"
}