title: Java extract from nested JSON
body: <p>I want to use financial data from yahoo in my program, it already works. I get the complete JSON content and I can display it. But now I want to <strong>extract</strong> the price as <strong>int</strong>. </p>
  
  <pre><code>public class Main {
  public static void main (String[]args) throws IOException {
  
  
  String sURL = "http://finance.yahoo.com/webservice/v1/symbols/googl/quote?format=json"; //just a string
  
  // Connect to the URL using java's native library
  URL url = new URL(sURL);
  HttpURLConnection request = (HttpURLConnection) url.openConnection();
  request.connect();
  
  // Convert to a JSON object to print data
  JsonParser jp = new JsonParser(); //from gson
  JsonElement root = jp.parse(new InputStreamReader((InputStream) request.getContent())); //convert the input stream to a json element
  JsonObject rootobj = root.getAsJsonObject(); //may be an array, may be an object.
  
      System.out.print(rootobj);
  
  }
  }
  </code></pre>
  
  <p><strong>EDIT</strong>
  This is the JSON data from yahoo</p>
  
  <pre><code>{
  "list" : { 
  "meta" : { 
  "type" : "resource-list",
  "start" : 0,
  "count" : 1
  },
  "resources" : [ 
  {
  "resource" : { 
  "classname" : "Quote",
  "fields" : { 
  "name" : "Google Inc.",
  "price" : "554.520020",
  "symbol" : "GOOGL",
  "ts" : "1432324800",
  "type" : "equity",
  "utctime" : "2015-05-22T20:00:00+0000",
  "volume" : "1213288"
  }
  }
  }
  
  ]
  }
  }
  </code></pre>
  
  <p><strong>EDIT 2</strong></p>
  
  <p>I changed my code</p>
  
  <pre><code>   JsonParser jp = new JsonParser(); //from gson
      JsonElement root = jp.parse(new InputStreamReader((InputStream) request.getContent())); //convert the input stream to a json element
          JsonObject obj = root.getAsJsonObject();
          JsonObject result = obj.get("list").getAsJsonObject();
          String result2 = result.get("resources").toString();
  
  
         System.out.print(result2);
  </code></pre>
  
  <p>And now I already get this</p>
  
  <pre><code>[{"resource":{"classname":"Quote","fields":{"name":"Google Inc.","price":"554.520020","symbol":"GOOGL","ts":"1432324800","type":"equity","utctime":"2015-05-22T20:00:00+0000","volume":"1213288"}}}]
  </code></pre>
  
  <p>How can I get the "price" now?  </p>
  
  <p><strong>EDIT 3</strong></p>
  
  <p>Ok I got it now, it works and I only get the price as double, but is this a smart way to solve this task?</p>
  
  <pre><code>// Convert to a JSON object to print data
  JsonParser jp = new JsonParser(); //from gson
  JsonElement root = jp.parse(new InputStreamReader((InputStream) request.getContent())); //convert the input stream to a json element
      JsonObject obj = root.getAsJsonObject();
      JsonObject result = obj.get("list").getAsJsonObject();
      JsonArray result2 = result.get("resources").getAsJsonArray();
      JsonObject result3 = result2.get(0).getAsJsonObject();
      JsonObject result4 = result3.get("resource").getAsJsonObject();
      JsonObject result5 = result4.get("fields").getAsJsonObject();
  
      String result6 = result5.get("price").toString();
      result6 = result6.replace("\"", "");
      double value = Double.parseDouble(result6);
  
  
     System.out.print(value);
  </code></pre>