我在这里使用的很多东西都不是很有经验。这是一个学校项目,我们将在其中构建使用语义结构化数据的软件。
我正在从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模型?
答案 0 :(得分:0)
Jena支持JSON-LD读写。
我们无法查看您尝试读取的输入,因为它位于登录名后面,但是通常,如果存在@context
链接,则解析器需要检索该上下文或JSON -LD无法正确读取。
上下文的URL为https://frost.met.no/schema
,但据我所知,这是网页的URL,并且那里没有发布JSON-LD上下文。因此,Frost API似乎有问题。
您始终可以将响应视为普通的JSON ...