我想从不同的博客中提取数据.我使用文章提取器完成它,但现在我必须将其转换为json格式以存储到MongoDB中.我的程序将整篇文章作为字符串返回如下:
String news=ArticleExtractor.INSTANCE.getText(doc);
如何将其转换为json格式?
URL url; url = new URL("http://blogs.timesofindia.indiatimes.com/mellowdrama/entry/india-needs-a-law-against-community-crime"); InputSource is = HTMLFetcher.fetch(url).toInputSource(); BoilerpipeSAXInput in = new BoilerpipeSAXInput(is); TextDocument doc = in.getTextDocument(); news=ArticleExtractor.INSTANCE.getText(doc); System.out.println(news); JSONObject jsonObj = new JSONObject(news);
最后一行显示错误......错误是
Exception in thread "main" java.lang.Error: Unresolved compilation problem: The constructor JSONObject(String) is undefined at Article_ext.main(Article_ext.java:39)
Kamlesh Arya.. 6
您可以尝试JSONObject
将String转换为Json格式
试试这种方式:
JSONObject jsonObj = new JSONObject("Your String");
使用org.json