How to get array of objects from JSON response using GSON
<p>I am writing a REST client using RestTemplate and GSON.
Below is sample of my JSON response</p>

<pre><code>{
  "value": [
    {
      "properties": {
        "vmId": "f7f953fb-d853-4373-b564-",
        "hardwareProfile": {
          "vmSize": "Standard_D2"
        },

        },
        "name": "A",
        "Id": ""
    },

    {
      "properties": {
        "vmId": "f7f953fb-d853-4373-b564-",
        "hardwareProfile": {
          "vmSize": "Standard_D2"
        },

        },
        "name": "B",
        "Id": ""
    },

    {
      "properties": {
        "vmId": "f7f953fb-d853-4373-b564-",
        "hardwareProfile": {
          "vmSize": "Standard_D2"
        },

        },
        "name": "C",
        "Id": ""
    }
    ]
}
</code></pre>

<p>What I want to is that I want to get only the values for the property --> "<code>name</code>"</p>

<p>So I created a simple POJO that has only <code>name</code> as the member field.</p>

<pre><code>public class VMNames {
    public String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}
</code></pre>

<p>and I am trying to use the GSON like this to get a array of this POJO. Here, the response is my JSON response object.</p>

<pre><code>Gson gson = new Gson();
VMNames[] vmNamesArray = gson.fromJson(response.getBody(), VMNames[].class);
System.out.println(vmNamesArray.length);
</code></pre>

<p>But when I do this, I get an error i.e. as below:</p>

<blockquote>
  <p>java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $</p>
</blockquote>

<p>Please note that I don't want to create a POJO that has exactly same structure as my JSON object because I want to get only one attribute out of my JSON object. I am hoping that I won't have to really create a POJO with the same structure as my JSON response because, in reality, it's a huge response and I don't control it, so it can also change tomorrow.</p>