Search code examples
javajsonboilerpipe

Boilerpipe - How do I output JSON?


I am using boilerpipe and it seems great, but I want to output JSON. I am using the Java version and testing in NetBeans as follows:

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

Can anyone tell me how I go about this?


Solution

  • Boilerpipe does not come with a JSON serializer.

    You can, however, do this (assuming you already extracted all data):

    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\" " +
               "}"
    }
    

    The tricky part will be of course getting the title...

    Or better yet use some JSON serializer like JSONObject.

    Hope that helps.