Jump to content

Photo

google finance java

google finance java api json

  • Please log in to reply
No replies to this topic

#1 Champion of Cyrodiil

Champion of Cyrodiil

    Gigabyte

  • Members
  • 776 posts
  • LocationVirginia

Posted 05 June 2013 - 09:35 PM

private static String defaultBaseUrl = "http://www.google.com/finance/info?infotype=infoquoteall&q=";
private static String defaultSymbolsFile = System.getProperty("user.home")
+ "/Desktop/NasdaqSymbols.csv";
private static boolean friendlyPrint = true;


public static void main(String[] args) throws Exception {


ArrayList<String> symbols = FileUtils.getTextListValues(defaultSymbolsFile);

StringBuilder syms = new StringBuilder();
//lets do the 1st 100
for (int i = 0; i < 100; i++) {
syms.append(symbols.get(i) + ",");
}

String sb = Util.fetchURL(defaultBaseUrl + syms);

JSONArray json = (JSONArray) new JSONParser().parse(sb.substring(3));

console.utils.GoogleFinancePrinter.printJsonArray(json, friendlyPrint);

}


public static String fetchURL(String url) throws Exception {
URL gf = new URL(url);
URLConnection yc = gf.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(
yc.getInputStream()));
StringBuilder sb = new StringBuilder();
String inputLine;
while ((inputLine = in.readLine()) != null)
sb.append(inputLine);
in.close();

return sb.toString();
}


public static void printJsonArray(JSONArray json, boolean friendly) {

if (friendly) {

for (int i = 0; i < json.size(); i++) {
System.out.println("ID: "
+ ((JSONObject) json.get(i)).get("id"));
System.out.println("Symbol: "
+ ((JSONObject) json.get(i)).get("t"));
System.out.println("Name: "
+ ((JSONObject) json.get(i)).get("name"));
System.out.println("Type: "
+ ((JSONObject) json.get(i)).get("type"));
System.out.println("Exchange: "
+ ((JSONObject) json.get(i)).get("e"));
System.out.println("LastTrade: $"
+ ((JSONObject) json.get(i)).get("l"));
System.out.println("Last Trade: "
+ ((JSONObject) json.get(i)).get("lt"));
System.out.println("Change: "
+ ((JSONObject) json.get(i)).get("c") + "("
+ ((JSONObject) json.get(i)).get("cp") + "%)");
System.out.println("------------------------------");
}
}
else {
System.out.print(json.toJSONString());
}
}

 







Also tagged with one or more of these keywords: google, finance, java, api, json