JSONObject json = new JSONObject(yourdata);
String statistics = json.getString("statistics");
JSONObject name1 = json.getJSONObject("John");
String ageJohn = name1.getString("Age");
For getting those items in a dynamic way:
JSONObject json = new JSONObject(yourdata);
String statistics = json.getString("statistics");
for (Iterator key=json.keys();key.hasNext();) {
JSONObject name = json.get(key.next());
//now name contains the firstname, and so on...
}
Answer from Emanuel on Stack OverflowParsing JSON object using Java
Can't import "json-20240303" library into my project.
Cannot configure JSON in eclipse (The type org.json.JSONObject is not accessible)
How to parse json to get size of the list using jsonObject in java?
Videos
JSONObject json = new JSONObject(yourdata);
String statistics = json.getString("statistics");
JSONObject name1 = json.getJSONObject("John");
String ageJohn = name1.getString("Age");
For getting those items in a dynamic way:
JSONObject json = new JSONObject(yourdata);
String statistics = json.getString("statistics");
for (Iterator key=json.keys();key.hasNext();) {
JSONObject name = json.get(key.next());
//now name contains the firstname, and so on...
}
You did not specify which library you intend to use to represent the JSON object. Usually there are methods to enumerate the properties of the object. For example:
org.json.JSONObject.keys()
returns an iterator of the String names in the object.
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