我正在尝试将stanford-nlp输出传递给json。我尝试使用simple-json,一切正常。当我尝试使用如下所示的javax.json时
public static void main(String[] args) throws IOException {
JsonObjectBuilder parserJSONObj = stanfordNLPParser(processedQuestion);
System.out.println(parserJSONObj.toString());
}
public static JsonObjectBuilder stanfordNLPParser (String processedQuestion)throws IOException {
Properties props = new Properties();
props.setProperty("annotators", "tokenize, ssplit, pos, lemma, ner, parse, sentiment");
StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
JsonObjectBuilder parserJSONObj = Json.createObjectBuilder();
Annotation annotation = pipeline.process(processedQuestion);
List<CoreMap> sentences = annotation.get(CoreAnnotations.SentencesAnnotation.class);
for (CoreMap sentence : sentences) {
for (CoreMap quesGentokens : sentence.get(TokensAnnotation.class)) {
String quesGenToken = quesGentokens.toString();
String quesGenPOS = quesGentokens.get(PartOfSpeechAnnotation.class);
parserJSONObj.add(quesGenToken, Json.createArrayBuilder()
.add(Json.createObjectBuilder()
.add("POS", quesGenPOS)));
parserJSONObj.build();
System.out.println(parserJSONObj.toString());
}
}
return parserJSONObj;
}
输出:
org.glassfish.json.JsonObjectBuilderImpl@6eb82908
我现在对此几乎没有问题:
由于
答案 0 :(得分:0)
我认为您应该从此方法返回JsonObject
。
您可以使用以下网址获取JsonObject
:
JsonObject json = parserJSONObj.build();
如果您在此json上执行toString()
,您将在控制台上打印实际的JSON。