[英]How to save JENA Sparql Query ResultSet as JSON?
How do I store the JENA ResultSet as JSON formatted string? 如何将JENA ResultSet存储为JSON格式的字符串? I'm currently only able to get the ResultSet to output to the System.out console, but I can't save that to a java String. 我目前只能将ResultSet输出到System.out控制台,但我无法将其保存到java String。 This is an example of where I'm at: 这是我所在的一个例子:
QueryExecution qexec = QueryExecutionFactory.sparqlService(endpoint, query);
ResultSet results = qexec.execSelect();
// the following prints out JSON in the System.out console:
ResultSetFormatter.outputAsJSON(System.out, results);
// but how do I save it as a String?
// ie.
String json = ResultSetFormatter.outputAsJSON(System.out, results);
// obviously that doesn't work, but how would one get the equivalent working version?
I want to be able to send the JSON variable to another method to perform some work on it. 我希望能够将JSON变量发送到另一个方法来执行一些工作。
Thanks in advance! 提前致谢!
Try writing to a ByteArrayOutputStream
and converting the bytes from that to a String
尝试写入ByteArrayOutputStream
并将其中的字节转换为String
QueryExecution qexec = QueryExecutionFactory.sparqlService(sparqlEndpointQuery, query);
ResultSet results = qexec.execSelect();
// write to a ByteArrayOutputStream
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ResultSetFormatter.outputAsJSON(outputStream, results);
// and turn that into a String
String json = new String(outputStream.toByteArray());
System.out.println(json);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.