anderssinho
anderssinho

Reputation: 298

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".

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. 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:

[
{
    "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);

Upvotes: 0

Views: 4886

Answers (2)

anderssinho
anderssinho

Reputation: 298

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. So now I start with creating an JSONArray and fill it with the information from the reader. Then the array contains just a JSONObject, so then I can go in there and grab abstractNote.

Upvotes: 1

Akhil Jayakumar
Akhil Jayakumar

Reputation: 2312

try this

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

Upvotes: 1

Related Questions