title: Parsing JSON by using gson
body: <pre><code>{
      "took": 6200,
      "timed_out": false,
      "_shards": {
          "total": 68,
          "successful": 68,
          "failed": 0
      },
      "hits": {
          "total": 110745094,
          "max_score": 1,
          "hits": []
      },
      "facets": {
          "pie": {
              "_type": "terms",
              "missing": 135,
              "total": 29349,
              "other": 26420,
              "terms": [
                  {
                      "term": "165.130.136.210",
                      "count": 390
                  },
                  {
                      "term": "165.130.136.206",
                      "count": 381
                  },
                  {
                      "term": "205.138.114.8",
                      "count": 359
                  },
                  {
                      "term": "205.138.115.229",
                      "count": 334
                  },
                  {
                      "term": "165.130.136.208",
                      "count": 331
                  },
                  {
                      "term": "66.37.212.155",
                      "count": 283
                  },
                  {
                      "term": "209.67.71.137",
                      "count": 279
                  },
                  {
                      "term": "66.37.204.17",
                      "count": 201
                  },
                  {
                      "term": "64.28.92.213",
                      "count": 193
                  },
                  {
                      "term": "64.85.64.202",
                      "count": 178
                  }
              ]
          }
      }
  }
  </code></pre>
  
  <p>I am trying to parse the following, I've tried many API, by using JSon in perl and JSon.simple, gson in Java without luck. </p>
  
  <p>What I am trying to parse out is facets=>pie=>terms=>term and count, can someone give me a hint on how to extract both values? </p>
  
  <p>Here is what I have so far</p>
  
  <pre><code>    import java.io.BufferedReader;
      import java.io.FileNotFoundException;
      import java.io.FileReader;
      import java.io.IOException;
  
  import com.google.gson.JsonArray;
  import com.google.gson.JsonElement;
  import com.google.gson.JsonObject;
  import com.google.gson.JsonParser;
  
  public class TrueIPMonitor {
  
      public static void main(String[] args) {
  
          String json = getJson();
          System.out.println(json);
          System.out.println(parse(json));
  
      }
  
      public static String parse(String jsonLine) {
          JsonElement jelement = new JsonParser().parse(jsonLine);
          JsonObject jobject = jelement.getAsJsonObject();
          jobject = jobject.getAsJsonObject("facets");
          JsonArray jarray = jobject.getAsJsonArray("pie");
          jobject = jarray.get(0).getAsJsonObject();
          String result = jobject.get("terms").toString();
          return result;
      }
  
      public static String getJson() {
          BufferedReader br;
          String json = "";
          try {
  
              br = new BufferedReader(new FileReader("script/curlJson.log"));
              StringBuilder sb = new StringBuilder();
              String line = br.readLine();
  
              while (line != null) {
                  sb.append(line);
                  sb.append('\n');
                  line = br.readLine();
              }
              json = sb.toString();
  
              try {
                  br.close();
              } catch (IOException e) {
                  e.printStackTrace();
              }
          } catch (FileNotFoundException e) {
              e.printStackTrace();
          } catch (IOException e) {
              e.printStackTrace();
          }
  
          return json;
  
      }
  }
  
  
  { "took" : 6200, "timed_out" : false, "_shards" : { "total" : 68, "successful" : 68, "failed" : 0 }, "hits" : { "total" : 110745094, "max_score" : 1.0, "hits" : [ ] }, "facets" : { "pie" : { "_type" : "terms", "missing" : 135, "total" : 29349, "other" : 26420, "terms" : [ { "term" : "165.130.136.210", "count" : 390 }, { "term" : "165.130.136.206", "count" : 381 }, { "term" : "205.138.114.8", "count" : 359 }, { "term" : "205.138.115.229", "count" : 334 }, { "term" : "165.130.136.208", "count" : 331 }, { "term" : "66.37.212.155", "count" : 283 }, { "term" : "209.67.71.137", "count" : 279 }, { "term" : "66.37.204.17", "count" : 201 }, { "term" : "64.28.92.213", "count" : 193 }, { "term" : "64.85.64.202", "count" : 178 } ] } } }
  
  Exception in thread "main" java.lang.ClassCastException: com.google.gson.JsonObject cannot be cast to com.google.gson.JsonArray
      at com.google.gson.JsonObject.getAsJsonArray(JsonObject.java:172)
      at com.xxx.perf.monitor.TrueIPMonitor.parse(TrueIPMonitor.java:36)
      at com.xxx.perf.monitor.TrueIPMonitor.main(TrueIPMonitor.java:28)
  </code></pre>