I have the following code:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Iterator;
import org.json.JSONArray;
import org.json.JSONObject;
public class Test {
public static void main(String[] args) {
try {
Test.call_me();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void call_me() throws Exception {
String url = "https://www.googleapis.com/youtube/v3/videos?id=Ald6YIXwiyI&key=AIzaSyBMHhfr4Crs6OvrV7nEnWWSF7bmRDHkgOg&part=statistics&fields=items(id,statistics)";
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
// optional default is GET
con.setRequestMethod("GET");
//add request header
con.setRequestProperty("User-Agent", "Mozilla/5.0");
int responseCode = con.getResponseCode();
System.out.println("\nSending 'GET' request to URL : " + url);
System.out.println("Response Code : " + responseCode);
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
//print in String
//System.out.println(response.toString());
//Read JSON response and print
JSONObject myResponse = new JSONObject(response.toString());
//System.out.println(response.toString());
System.out.println(myResponse.toString());
JSONObject items = new JSONObject(myResponse.getJSONArray("items").getJSONArray(0));
Now this generates a Json of the following format:
{"items":[{"id":"Ald6YIXwiyI","statistics": {"dislikeCount":"46","likeCount":"3483","viewCount":"43745","favoriteCount":"0","commentCount":"499"}}]}
My question is how can I save this Json in an ArrayList or in a data structure, check in a Json editor online and I have an array structure in the code I try to access using a:
getJSONArray("items")
But it only returns me a position and it is blank