【发布时间】:2016-06-23 11:21:26
【问题描述】:
我正在尝试将 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
我现在对此有几个问题:
- 即使添加 toString 也只显示内存位置。
- 是JsonObjectBuilder的返回类型正确的返回类型(考虑,对Json进一步操作)。
谢谢
【问题讨论】:
-
您正在打印构建器,而不是构建器创建的 JSON 对象。