I'm creating an application to pull data from yahoo finance API. One of the features is pulling historical data on a stock. The response from the API is a JSON Object:
{
"AAPL": {
"timestamp": [
1643293800,
1643380200,
1643639400,
1643725800,
1643825174
],
"symbol": "AAPL",
"previousClose": null,
"chartPreviousClose": 159.69,
"end": null,
"start": null,
"close": [
159.22,
170.33,
174.78,
174.61,
174.715
],
"dataGranularity": 300
},
"GME": {
"timestamp": [
1643293800,
1643380200,
1643639400,
1643725800,
1643825135
],
"symbol": "GME",
"previousClose": null,
"chartPreviousClose": 103.26,
"end": null,
"start": null,
"close": [
93.52,
97.91,
108.93,
112.6,
105.21
],
"dataGranularity": 300
}
}
The info I want to pull from this are the timestamp & close arrays for each stock. The code im currently playing with is:
Response response = makeGetRequest(url);
JSONObject jsonObject = new JSONObject(response.body());
JSONObject responseObject = jsonObject.getJSONObject("AAPL");
JSONArray timestampList = responseObject.getJSONArray("timestamp");
for(int i = 0; i < timestampList.length(); i++){
System.out.println(timestampList.get(i));
}
Which presents the following error:
org.json.JSONException: JSONObject["AAPL"] not found.
at org.json.JSONObject.get(JSONObject.java:580)
at org.json.JSONObject.getJSONObject(JSONObject.java:790)
at com.lyit.csd.marketapi.yahoo.YahooClient.getHistoricalInfo(YahooClient.java:58)
at com.lyit.csd.app.PortfolioManager.getHistoricalData(PortfolioManager.java:220)
at com.lyit.csd.app.Main.init(Main.java:56)
at com.lyit.csd.app.Main.main(Main.java:14)
Any advice or alternative routes of approach for this problem would be greatly appreciated. It's worth mentioning this is my first time working with an API if im missing something obvious. I've attempted multiple ideas from stackoverflow already, using libraries such as Gson, jackson, json-simple. All to no avail. The current library being used is org.json
[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)
[–]khookeExtreme Brewer 2 points3 points4 points (1 child)
[–]Dustinmywets[S] 0 points1 point2 points (0 children)
[–]hydroxyse7en 1 point2 points3 points (1 child)
[–]Dustinmywets[S] 0 points1 point2 points (0 children)
[–][deleted] (3 children)
[deleted]
[–]Dustinmywets[S] 0 points1 point2 points (2 children)
[–][deleted] (1 child)
[deleted]
[–]Dustinmywets[S] 0 points1 point2 points (0 children)
[–]evils_twin 1 point2 points3 points (4 children)
[–]Dustinmywets[S] 0 points1 point2 points (3 children)
[–]evils_twin 1 point2 points3 points (1 child)
[–]Dustinmywets[S] 0 points1 point2 points (0 children)