Advertisement
Guest User

Untitled

a guest
Jan 29th, 2014
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.09 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.net.HttpURLConnection;
  5. import java.net.URL;
  6.  
  7. public class JSONParser {
  8.  
  9.     public static void main(String args[]) throws IOException {
  10.  
  11.         System.out.println("Enter Stock Symbol: ");
  12.         String stockSymbol = new String(new BufferedReader(
  13.                 new InputStreamReader(System.in)).readLine());
  14.         String jsonString = jsonURLToString(stockSymbolToURL(stockSymbol));
  15.         System.out.println(getJsonKeyValue(jsonString, "change"));
  16.     }
  17.  
  18.     static String jsonURLToString(String urlString) throws IOException {
  19.  
  20.         URL url = new URL(urlString);
  21.  
  22.         HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
  23.         if (httpConn.getResponseCode() == HttpURLConnection.HTTP_OK) {
  24.  
  25.             String jsonString = new BufferedReader(new InputStreamReader(
  26.                     url.openStream())).readLine().toString();
  27.  
  28.             return jsonString.trim();
  29.         } else {
  30.             return "Could not connect";
  31.         }
  32.     }
  33.  
  34.     static String stockSymbolToURL(String stockSymbol) {
  35.  
  36.         String urlString = "http://query.yahooapis.com/v1/public/yql?"
  37.                 + "q=select%20*%20from%20yahoo.finance.quote%20where%20"
  38.                 + "symbol%20in%20(%22" + stockSymbol
  39.                 + "%22)&format=json&diagnostics=t"
  40.                 + "rue&env=store%3A%2F%2Fdatatables.org%2Falltableswith"
  41.                 + "keys&callback=";
  42.         return urlString;
  43.     }
  44.  
  45.     static String getJsonKeyValue(String jsonString, String key) {
  46.  
  47.         String errorMessage = "getJsonArray: first argument does not contain second argument";
  48.         String jsonValue = new String("");
  49.  
  50.         if (jsonString.contains(key)) {
  51.  
  52.             for (int i = 0; i < jsonString.length() - 1; i++) {
  53.  
  54.                 if (jsonString.charAt(i) == '\"') {
  55.  
  56.                     if (jsonString.substring(i + 1, i + key.length() + 1)
  57.                             .equalsIgnoreCase(key)) {
  58.  
  59.                         i += key.length() + 1;
  60.  
  61.                         if (jsonString.charAt(i) == '\"') {
  62.                             i += 3;
  63.  
  64.                             while (jsonString.charAt(i) != '\"') {
  65.  
  66.                                 jsonValue += jsonString.charAt(i);
  67.                                 i++;
  68.                             }
  69.                         }
  70.                         return jsonValue;
  71.                     }
  72.                 }
  73.             }
  74.             return jsonValue;
  75.         } else {
  76.             return errorMessage;
  77.         }
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement