javax json toString仍然返回内存值

时间:2016-06-23 11:21:26

标签: java json java-7 tostring

我正在尝试将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

我现在对此几乎没有问题:

  1. 甚至添加toString只显示内存位置。
  2. 是返回类型JsonObjectBuilder的右返回类型(考虑到Json上的进一步操作)。
  3. 由于

1 个答案:

答案 0 :(得分:0)

我认为您应该从此方法返回JsonObject。 您可以使用以下网址获取JsonObject

JsonObject json = parserJSONObj.build();    

如果您在此json上执行toString(),您将在控制台上打印实际的JSON。