Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中解析来自BufferedReader的传入字符串_Java_Json_Parsing - Fatal编程技术网

在Java中解析来自BufferedReader的传入字符串

在Java中解析来自BufferedReader的传入字符串,java,json,parsing,Java,Json,Parsing,我正在使用一个程序,在翻译的帮助下从数据库收集信息 我已经建立了连接并重定向到工作中,但在我想解析传入信息以获取所需信息的部分上遇到了问题。在这种情况下,它被称为“abstractNote” 我尝试使用JSON解析器,因为我认为从缓冲区传入的是JSON对象。但是当我这样做的时候,我不能抓住一些东西,而只是不断得到空结果 我能做什么 以下是JSON结构,我相信: [ { "itemType": "journalArticle", "creators": [{"firstName"

我正在使用一个程序,在翻译的帮助下从数据库收集信息

我已经建立了连接并重定向到工作中,但在我想解析传入信息以获取所需信息的部分上遇到了问题。在这种情况下,它被称为“abstractNote”

我尝试使用JSON解析器,因为我认为从缓冲区传入的是JSON对象。但是当我这样做的时候,我不能抓住一些东西,而只是不断得到空结果

我能做什么

以下是JSON结构,我相信:

[
{
    "itemType": "journalArticle",

    "creators": [{"firstName":"Xudong","lastName":"Song","creatorType":"author"},
            {"firstName":"Xiaobing","lastName":"Liu","creatorType":"author"}],

    "notes":[],
    "tags":[],
    "title":"An approach for designing, modeling and realizing etl processes based on unified views model",
    "date":"June 1, 2011",
    "DOI":"10.1142/S0218194011005402",

    "publicationTitle":"International Journal of Software Engineering and Knowledge Engineering",

    "journalAbbreviation":"Int. J. Soft. Eng. Knowl. Eng.",
    "pages":"543-570",

    "volume":"21",
    "issue":"04",
    "ISSN":"0218-1940",
    "url":"http://www.worldscientific.com/doi/abs/10.1142/S0218194011005402",

"abstractNote":"Extraction-Transformation-Loading (ETL) tools are pieces of software responsible for the extraction of data from
several sources, their cleaning, customization and insertion into Data Warehouses (DWs). Complexity, usability and maintainability are  the primary problems concerning ETL processes. To deal with these problems, in this paper we provide a dynamic approach for designing, modeling and realizing ETL processes. We propose a new architecture based on Unified Views Model (UVM) for ETL processes, in which Unified view layer is added between source data level and DWs level. The unified views model serves as the means to conform the structure and semantics of the source data to the ones of the data warehouses, and help designers understand and analyze the meaning, relationships and lineage of information. In order to guarantee the transparency access and the usability, two mapping methods are adopted between Unified view level and source data level as well as between DWs level and Unified view level. Based on this architecture, the method of constructing UVM and ETL operations among three levels is given. Then, we describe how to build the conceptual modeling for ETL processes based on UVM by using an extension of the Unified Modeling Language (UML). Finally, we present an ETL tool based on UVM (UVETL) with the goal of facilitating the design, modeling and realization of ETL processes, and give a case study to exemplify the benefits of our proposal.",

"libraryCatalog":"worldscientific.com (Atypon)",
"accessDate":"CURRENT_TIMESTAMP"}

]
这是我试图解析的代码之一:

System.out.println(line);
JSONObject obj = new JSONObject(line);
String abstracts = bj.getJSONObject("itemType").getString("abstractNote");
System.out.println(abstracts);
试试这个

    line= line.replace("[", " ");
    line= line.replace("]", " ");
    JSONObject obj = new JSONObject(line);
    String abstracts = bj.getJSONObject("itemType").getString("abstractNote");
    System.out.println(abstracts);
问题是我没有意识到这是一个JSONArray而不是JSONObject。
现在我从创建一个JSONArray开始,并用读者提供的信息填充它。然后数组只包含一个JSONObject,这样我就可以进去获取abstractNote。

但是当我这样做的时候,我无法获取一些东西——这只是一个问题,而不是另一个代码。请说明您如何尝试从json输入中提取元素。上面的json代码不是json对象,而是json数组。Okej添加了我的一个代码。这一个只会在abstractyou上给我一个空结果,不能生成json对象,因为line是json arrayOkay,所以我应该先生成json数组,然后获取数组?abstractNote只是JSON数组中的一个对象吗?但是我的数组叫什么?不要在上面加任何名字。现在已经试过你的例子:System.out.println(line);line=line.replace(“[”,”);line=line.replace(“]”,”);JSONObject obj=新的JSONObject(行);String abstracts=obj.getJSONObject(“itemType”).getString(“abstractNote”);系统输出打印(摘要);文章摘要(摘要);DatabaseHandler.GetInstance().UpdateArticle(文章);但我还是没有得到任何摘要。我可以在我的mongoDB中确认抽象仍然为空。我做错了什么?尝试从第行装箱一个json数组,并按数组位置获取json对象。然后可以从json对象调用abstractNote。首先创建数组:JSONArray jsonArr=new JSONArray(第行);但是我怎么知道对象abstractNote在什么位置呢?你的数组只有一个json对象,所以试试这个
JSONObject object=jsonArr.getJSONObject(0)因为json对象是数组中唯一也是第一个对象。非常感谢!现在我终于得到了摘要:D
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import org.json.*;

public class ZoteroHandler {

    public static void Scan(Article article) throws Exception
    {
        URL urlDoi = new URL (article.GetElectronicEdition());
        HttpURLConnection connDoi = (HttpURLConnection)  urlDoi.openConnection();

        // Make the logic below easier to detect redirections
        connDoi.setInstanceFollowRedirects(false);  

        String doi = "{\"url\":\"" + connDoi.getHeaderField("Location") + "\",\"sessionid\":\"abc123\"}";
        String urlParameters = doi;
        URL url = new URL("http://127.0.0.1:1969/web");
        URLConnection conn = url.openConnection();

        conn.setDoOutput(true);
        conn.setRequestProperty("Content-Type", "application/json");

        OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());

        writer.write(urlParameters);
        writer.flush();

        String line;

        BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));

        while ((line = reader.readLine()) != null) 
        {
            System.out.println(line);

        }

        writer.close(); 
        reader.close();         
    }
while ((line = reader.readLine()) != null) 
        {
            System.out.println(line);
            JSONArray jsonArr = new JSONArray(line);
            JSONObject obj = jsonArr .getJSONObject(0);
            String abstracts = obj.getString("abstractNote");
            System.out.println(abstracts);
            article.SetAbstracts(abstracts);
            DatabaseHandler.GetInstance().UpdateArticle(article);

        }