title: Using Gson with a path
body: <p>Using a simple Json file e.g:</p>
  
  <pre><code>{"menu": {
    "id": "file",
    "value": "File",
    "popup": {
      "menuitem": [
        {"value": "New", "onclick": "CreateNewDoc()"},
        {"value": "Open", "onclick": "OpenDoc()"},
        {"value": "Close", "onclick": "CloseDoc()"}
      ]
    }
  }}
  </code></pre>
  
  <p>I want to be able to get the <code>JsonArray</code> named <code>menuitem</code> using a path:</p>
  
  <pre><code>String path =  "menu.popup.menuitem"
  </code></pre>
  
  <p>I tried to do this using:</p>
  
  <pre><code>public static JsonElement fromString(String json, String path) throws JsonSyntaxException {
          JsonObject obj = GsonBuilder.create().fromJson(json, JsonObject.class);
          String[] seg = path.split(".");
          for (String element : seg) {
              if (obj != null) {
                  obj = obj.get(element).getAsJsonObject();
              } else {
                  return null;
              }
          }
          return obj
  }
  </code></pre>
  
  <p>with:</p>
  
  <pre><code>JsonElement jsonElement = fromString(json, path);
  </code></pre>
  
  <p>But when I try <code>isJsonArray()</code> the return value is <code>false</code>. When doing the extra sanity check using <code>Gson.toJson(jsonElement)</code> the output is the full json String (above) that was inputted originally.
  What's going wrong?</p>