簡體   English   中英

從Java中的BufferedReader解析傳入的字符串

[英]Parse incoming String from BufferedReader in Java

我正在使用一個在翻譯程序幫助下從數據庫收集信息的程序。

我已經有了連接並重定向到工作,但是卡在了我想解析傳入信息以僅抓取我需要的部分的那部分。 在這種情況下,它稱為“ abstractNote”。

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();         
    }

我嘗試使用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);
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);

        }

對我來說,問題是我沒有意識到這是一個JSONArray而不是一個JSONObject。 因此,現在我開始創建一個JSONArray並用來自讀者的信息填充它。 然后,該數組僅包含一個JSONObject,因此我可以進入其中並獲取abstractNote。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM