我在这里使用的很多东西都不是很有经验。这是针对我们要构建使用语义结构化数据的软件的学校项目。
我以json-ld格式从Frost api(https://frost.met.no/)获取气象数据。我想把它读成耶拿模型。我对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
},
还有更多,但它只是关于不同SensorSystems的更多数据。
我没有错误,但它输出的模型似乎是空的:
{
"@id" : "_:b0",
"@type" : "file:///C:/Users/bm_93/Desktop/Fag/INFO216/SemesterOppgave/SourceResponse"
}
我做得对吗?这是jena支持的吗?
如果没有,有什么办法可以将这个json数据变成jena模型吗?
Jena支持JSON-LD读写。
我们无法查看您尝试阅读的输入,因为它位于登录后面,但通常如果存在@context
链接,则解析器需要检索该上下文,否则将无法读取JSON-LD正确。
上下文的URL是https://frost.met.no/schema
,但据我所知,这是一个网页的URL,并且没有在那里发布的JSON-LD上下文。所以它看起来像Frost API的一个问题。
您始终可以将响应视为普通JSON ...