2012-09-27 60 views
5

我使用boilerpipe它看起來不錯,但我想輸出JSON。我使用NetBeans中的Java版本和測試如下:Boilerpipe - 我如何輸出JSON?

final URL url = new URL("http://mashable.com/2012/09/26/worlds-best-father-kickstarter-calendar"); 
System.out.println(ArticleExtractor.INSTANCE.getText(url)); 

誰能告訴我怎麼去呢?

+0

如果我的回答確實有幫助,善良並將其標記爲答案... –

回答

2

Boilerpipe不帶有JSON序列化程序。

你可以,但是,這樣做(假設你已經提取的所有數據):

public String articleTextToJson(String article, String title, String sourceUrl) { 
    if (null == article) { 
     return "{ \"error\" : { " + 
       "  \"message\" : \"Article did not extract\", " + 
       "  \"code\" : 1 " + 
       " }, " + 
       " \"status\" : \"error\" " + 
       "}"; 
    } 
    return "{ \"response\" : { " + 
      "  \"title\" : \"" + title + "\" " + 
      "  \"content\" : \"" + article + "\", " + 
      "  \"source\" : \"" + sourceUrl + "\" " + 
      " }, " + 
      " \"status\" : \"success\" " + 
      "}" 
} 

最棘手的部分當然會獲得稱號......

或者更好的是使用一些JSON串行器像JSONObject

希望有所幫助。

+0

乾杯,我將標題作爲參數傳遞 – Wadester

+0

太好了...您是否使用了這個建議的功能? –

+0

不,我有一個做我需要做的servlet。它使用提取的內容和傳入的標題作爲參數來構建一個json字符串。 – Wadester