有没有办法让 jena 将 Json-ld 读入模型?
Is there a way to get jena to read Json-ld into a model?
我在这里使用的很多东西都不是很有经验。这是一个学校项目,我们将在该项目中构建使用语义结构化数据的软件。
我正在从 Frost 获取气象数据 api (https://frost.met.no/)
json-ld 格式。我想将其读入 Jena 模型。我有点困惑jena是否支持这个。
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
},
还有很多,但只是关于不同传感器系统的更多数据。
我没有收到任何错误,但它输出的模型似乎是空的:
{
"@id" : "_:b0",
"@type" : "file:///C:/Users/bm_93/Desktop/Fag/INFO216/SemesterOppgave/SourceResponse"
}
我做对了吗? jena 支持这个吗?
如果没有,我能做些什么来将这个 json 数据导入 jena 模型吗?
Jena支持JSON-LD读写
我们无法查看您尝试读取的输入,因为它在登录后,但通常如果有 @context
link,则解析器需要检索该上下文否则 JSON-LD 将无法正确读取。
上下文的URL是https://frost.met.no/schema
,但据我所知,那是网页的URL,没有JSON -LD 上下文在那里发布。所以它看起来像是 Frost API.
的问题
您始终可以将响应视为正常 JSON...
我在这里使用的很多东西都不是很有经验。这是一个学校项目,我们将在该项目中构建使用语义结构化数据的软件。
我正在从 Frost 获取气象数据 api (https://frost.met.no/) json-ld 格式。我想将其读入 Jena 模型。我有点困惑jena是否支持这个。
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
},
还有很多,但只是关于不同传感器系统的更多数据。
我没有收到任何错误,但它输出的模型似乎是空的:
{
"@id" : "_:b0",
"@type" : "file:///C:/Users/bm_93/Desktop/Fag/INFO216/SemesterOppgave/SourceResponse"
}
我做对了吗? jena 支持这个吗?
如果没有,我能做些什么来将这个 json 数据导入 jena 模型吗?
Jena支持JSON-LD读写
我们无法查看您尝试读取的输入,因为它在登录后,但通常如果有 @context
link,则解析器需要检索该上下文否则 JSON-LD 将无法正确读取。
上下文的URL是https://frost.met.no/schema
,但据我所知,那是网页的URL,没有JSON -LD 上下文在那里发布。所以它看起来像是 Frost API.
您始终可以将响应视为正常 JSON...