简体   繁体   English

从Java中的BufferedReader解析传入的字符串

[英]Parse incoming String from BufferedReader in Java

I'm working with a program that is collecting information from a database with the help of a translator. 我正在使用一个在翻译程序帮助下从数据库收集信息的程序。

I have got the connection and redirect to work but got stuck on the part where I'm suppose to parse the incoming information to just grab the piece I need. 我已经有了连接并重定向到工作,但是卡在了我想解析传入信息以仅抓取我需要的部分的那部分。 In this case it's something called "abstractNote". 在这种情况下,它称为“ 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();         
    }

I have tried to use a JSON parser since I think the incoming from buffer is a JSON-object. 我尝试使用JSON解析器,因为我认为从缓冲区传入的是JSON对象。 But when I do that I can't grab something and just keep getting null results. 但是,当我这样做时,我无法抓住任何东西,而只能保持空结果。

What can I do? 我能做什么?

Here is the JSON structure, I believe: 我相信这是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"}

]

This is one of the codes I tried to parse with: 这是我尝试解析的代码之一:

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

try this 尝试这个

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

        }

The problem was for me that I didn't realise that it was a JSONArray and not a JSONObject. 对我来说,问题是我没有意识到这是一个JSONArray而不是一个JSONObject。 So now I start with creating an JSONArray and fill it with the information from the reader. 因此,现在我开始创建一个JSONArray并用来自读者的信息填充它。 Then the array contains just a JSONObject, so then I can go in there and grab abstractNote. 然后,该数组仅包含一个JSONObject,因此我可以进入其中并获取abstractNote。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM