从Java中的BufferedReader中解析传入的String

时间:2015-10-21 09:16:59

标签: java json parsing

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

我已经获得了连接并重定向工作,但却陷入了我想要解析传入信息以便抓住我需要的部分的部分。在这种情况下,它被称为" 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);

2 个答案:

答案 0 :(得分:1)

试试这个

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

答案 1 :(得分:1)

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。