在java中将字符串转换为json格式

 海豚青春_407 发布于 2023-01-31 14:07

我想从不同的博客中提取数据.我使用文章提取器完成它,但现在我必须将其转换为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

撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有