0

I have a website that I have to make queries on in Java. Here you can see the dataset

https://www.quandl.com/api/v3/datasets/SSE/HYQ.json?start_date=2017-01-01&end_date=2017-01-31

At the very first beginning, I thought this is a JSONArray, but Eclipse always told me, it is not.

So therefore I tried to convert from JSONObject to JSONArray but I get this error:

org.json.JSONException: JSONObject["dataset"] is not a JSONArray.

What am I doing wrong?

Here is my code:

package query;

 import java.net.URL;
 import java.net.URLConnection;
 import java.nio.charset.Charset;

 import java.util.*;
 import java.io.*;
 import org.json.JSONArray;
 import org.json.JSONException;
 import org.json.JSONObject;

public class Stockquery {

public static void main(String[] args) {

    String jsonString = callURL(
            "https://www.quandl.com/api/v3/datasets/SSE/HYQ.json?start_date=2017-01-01&end_date=2017-01-31");
    // System.out.println("\n\njsonString: " + jsonString);

    try {
        JSONObject jsonobjects = new JSONObject(jsonString);
        System.out.println("\n\njsonArray: " + jsonobjects);
        JSONArray arr = jsonobjects.getJSONArray("dataset");

        for (int i = 0; i < arr.length(); i++) {
            JSONObject obj = arr.getJSONObject(i); 
            System.out.println(obj);
        }

    } catch (JSONException e) {
        e.printStackTrace();
    }

}

public static String callURL(String myURL) {
    // System.out.println("Requested URL:" + myURL);
    StringBuilder sb = new StringBuilder();
    URLConnection urlConn = null;
    InputStreamReader in = null;
    try {
        URL url = new URL(myURL);
        urlConn = url.openConnection();
        if (urlConn != null)
            urlConn.setReadTimeout(60 * 1000);
        if (urlConn != null && urlConn.getInputStream() != null) {
            in = new InputStreamReader(urlConn.getInputStream(), Charset.defaultCharset());
            BufferedReader bufferedReader = new BufferedReader(in);
            if (bufferedReader != null) {
                int cp;
                while ((cp = bufferedReader.read()) != -1) {
                    sb.append((char) cp);
                }
                bufferedReader.close();
            }
        }
        in.close();
    } catch (Exception e) {
        throw new RuntimeException("Exception while calling URL:" + myURL, e);
    }

    return sb.toString();
}

}

1
  • dataset is jsonobject , what exactly your want to do with your response Commented Jun 26, 2017 at 18:04

1 Answer 1

2

The dataset field of jsonobjects is a JSONObject not JSONArray. Change this:

jsonobjects.getJSONArray("dataset");

to this:

JSONObject dataset = jsonobjects.getJSONObject("dataset");
JSONArray array    = dataset.getJSONArray("data");
Sign up to request clarification or add additional context in comments.

3 Comments

Are you sure? I just printed the JSON locally and 'data' is a JSONArray of JSONArrays in the 'dataset' JSONObject
Could you copy your code in your answer please, so that I can check what I missed, I have lost the red thread.
Yeah, now it works, thanks for the edit and for the answer of course :)

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.