Closed. This question needs details or clarity。它当前不接受答案。
                            
                        
                    
                
            
                    
                
                        
                            
                        
                    
                        
                            想改善这个问题吗?添加详细信息并通过editing this post阐明问题。
                        
                        6年前关闭。
                    
                
        

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

最佳答案

JSONObject jsonObj = new JSONObject();
jsonObj.append("news", news);

关于java - 将字符串转换为java中的json格式,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21349568/

10-11 13:28