【发布时间】:2023-04-03 21:01:01
【问题描述】:
我有一个问题
CONSTRUCT { ?highValForeignTran ?hvFTPred ?hvFTObj . }
WHERE { ?highValForeignTran vocab:accounttransactions_transactionCurrency "USD" .
?highValForeignTran vocab:accounttransactions_transactionValue ?tranValue .
?highValForeignTran vocab:accounttransactions_transactionDate ?tranDate .
?highValForeignTran ?hvFTPred ?hvFTObj .
FILTER ( ?tranValue > 10000) .
FILTER ( ?tranDate >= "2013-11-23"^^xsd:date && ?tranDate <= "2013-11-23"^^xsd:date) .
}
返回结果:
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:vocab="http://localhost:2020/resource/vocab/"
xmlns:owl="http://www.w3.org/2002/07/owl#"
xmlns:db="http://localhost:2020/resource/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
xmlns:map="http://localhost:2020/resource/#">
<vocab:accounttransactions rdf:about="http://localhost:2020/resource/accounttransactions/1">
<vocab:accounttransactions_id rdf:datatype="http://www.w3.org/2001/XMLSchema#integer"
>1</vocab:accounttransactions_id>
<vocab:accounttransactions_transactionCurrency>USD</vocab:accounttransactions_transactionCurrency>
<vocab:accounttransactions_originAccountNumber>DB48939239</vocab:accounttransactions_originAccountNumber>
<vocab:accounttransactions_transactionType>Cr</vocab:accounttransactions_transactionType>
<vocab:accounttransactions_transactionDate rdf:datatype="http://www.w3.org/2001/XMLSchema#date"
>2013-11-23</vocab:accounttransactions_transactionDate>
<vocab:accounttransactions_destinationAccountId rdf:resource="http://localhost:2020/resource/bankaccounts/1"/>
<vocab:accounttransactions_transactionValue rdf:datatype=
"http://www.w3.org/2001/XMLSchema#decimal">12000</vocab:accounttransactions_transactionValue>
<rdfs:label>accounttransactions #1</rdfs:label>
<vocab:accounttransactions_destinationAccountNumber>47321896544567</vocab:accounttransactions_destinationAccountNumber>
</vocab:accounttransactions>
</rdf:RDF>
当我尝试使用 Jena 解析它时,我只得到一个代表外部 accountTransactions 三元组的三元组:
{"http://localhost:2020/resource/accounttransactions/1":
{"subject":"http://localhost:2020/resource/accounttransactions/1",
"predicate":"http://www.w3.org/1999/02/22-rdf-syntax-ns#type",
"object":"http://localhost:2020/resource/vocab/accounttransactions"}
}
我不知道为什么其他三元组会嵌套在其中,但我确实需要能够解析它们并将它们作为 JSON 发送。 这是我的代码:
try {
Model result = qexec.execConstruct();
JSONObject jsonShell = new JSONObject();
StmtIterator stmtIter = model.listStatements();
while ( stmtIter.hasNext() ) {
Statement stmt = stmtIter.nextStatement();
JSONObject innerJson = new JSONObject();
innerJson.put("subject", stmt.getSubject().visitWith(rdfVisitor));
innerJson.put("predicate", stmt.getPredicate().visitWith(rdfVisitor));
innerJson.put("object", stmt.getObject().visitWith(rdfVisitor));
jsonShell.put(String.valueOf(stmt.getSubject().visitWith(rdfVisitor)), innerJson);
}
System.out.println(resultJson.toString());
}
finally {
qexec.close();
}
RDFVisitor rdfVisitor = new RDFVisitor() {
@Override
public Object visitURI(Resource r, String uri) {
return uri;
}
@Override
public Object visitLiteral(Literal l) {
return l.getLexicalForm();
}
@Override
public Object visitBlank(Resource r, AnonId id) {
return id.getLabelString();
}
};
我想知道Statement.getProperty() 是否可以解决问题,但找不到创建Property 实例的方法。
【问题讨论】:
-
其他三元组没有“嵌套在里面”。这只是 RDF/XML 的一种编写方式。 Turtle 中的 RDF 看起来像 pastebin.com/yU80Kqf8 。你得到了很好的数据。
-
我没用过 JSONObjects,但是
jsonShell.put("X", innerJson);会在 JSON 中添加一堆“X”元素,还是继续覆盖之前的?此模型中的所有三元组都具有相同的主题,因此如果您正在执行jsonShell.put(<subject-uri>,innerJson)并且覆盖了现有条目,那么您最终只会得到一个。 -
最好提及您正在使用的 JSON 库。我有一个加载了 Jena 的项目,但我没有任何 JSONObject 的自动完成功能……
-
@JoshuaTaylor:这是 Douglas Crockford 的 JSON-java。关于 put 覆盖,您可能是对的。我只是假设由于三元组是嵌套的,我的代码无法访问它们。我的错。明天早上我回到我的项目后会尽快验证这一点。非常感谢。