0

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);
5
  • But when I do that I can't grab something -- this is only that matters, not another code. Please show how you try to extract elements from json input. Commented Oct 21, 2015 at 9:27
  • 1
    The Above JSON code is not a JSON object it's a JSON array. Commented Oct 21, 2015 at 9:27
  • Okej added one of my tried codes. This one will just give me a null result on abstract Commented Oct 21, 2015 at 9:40
  • 1
    you can't make json object because line is a json array Commented Oct 21, 2015 at 9:44
  • Okay, so I should make an JSON array first then and grab the array? Is abstractNote just a object in the JSON array? But what is my array called? Don't se any name on it. Commented Oct 21, 2015 at 9:47

2 Answers 2

1

try this

    line= line.replace("[", " ");
    line= line.replace("]", " ");
    JSONObject obj = new JSONObject(line);
    String abstracts = bj.getJSONObject("itemType").getString("abstractNote");
    System.out.println(abstracts);
Sign up to request clarification or add additional context in comments.

5 Comments

Have tried your exampel now: System.out.println(line); line= line.replace("[", " "); line= line.replace("]", " "); JSONObject obj = new JSONObject(line); String abstracts = obj.getJSONObject("itemType").getString("abstractNote"); System.out.println(abstracts); article.SetAbstracts(abstracts); DatabaseHandler.GetInstance().UpdateArticle(article); But I still get nothing in abstracts. I can se that in my mongoDB that the abstract is still null. What I'm doing wrong?
Try to crate a json array from line and get the json object by array position.then you can call abstractNote from the json object.
Start by creating the array: JSONArray jsonArr = new JSONArray(line); But how do I know what position the object abstractNote is on?
your array have only one json object ,so try thisJSONObject object = jsonArr .getJSONObject(0); Because your json object is the only one and first object in array.
Thanks alot! Now I finally got the abstractNote :D
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);

        }

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.