title: How to parse complex JSON string mix with JSON Arrays using GSON?
body: <p>Below is my JSON String and I am trying to parse this JSON String using GSON as I am finding it pretty easy to work with.</p>
  
  <pre><code>{
  "HasChanged":true,
  "Version":1,
  "LastModifiedDate":1390561121310,
      "DATACENTERS":
          [
              {
              "Name": "DC1",
              "TotalNumberOfServers":4,
              "PrimaryData":[{"0":"1", "1":"2", "2":"3", "3":"4"}],
              "SecondaryData":[{"0":"2", "1":"3", "2":"4", "3":"1"}],
              "MachineMapping":[{"3":"dc11115.dc1.domain.com"}, {"2":"dc11114.dc1.domain.com"}, {"1":"dc11113.dc1.domain.com"}, {"4":"dc11116.dc1.domain.com"}]
              },
              {
              "Name": "DC2",
              "TotalNumberOfServers":4,
              "PrimaryData":[{"1":"1", "5":"2", "2":"3", "6":"4"}],
              "SecondaryData":[{"7":"1", "0":"8", "2":"5", "10":"9"}],
              "MachineMapping":[{"3":"dc21147.dc2.domain.com"}, {"2":"dc21146.dc2.domain.com"}, {"1":"dc21145.dc2.domain.com"}, {"4":"dc21148.dc2.domain.com"}]
              }
          ]
  }
  </code></pre>
  
  <p>After parsing the above JSON String, I need to store each <code>DataCenter</code> data in the below data structure - </p>
  
  <pre><code>Map&lt;String, Map&lt;String, String&gt;&gt; primaryData
  Map&lt;String, Map&lt;String, String&gt;&gt; secondaryData
  Map&lt;String, Map&lt;String, String&gt;&gt; machineMapping
  </code></pre>
  
  <p>For <code>PrimaryData</code> tag in the JSON String- </p>
  
  <pre><code>Here Key of `primaryData` map is `DC1` and value is {"0":"1", "1":"2", "2":"3", "3":"4"} which is for `PrimaryData` tag.
  Similarly, another Key of `primaryData` map is `DC2` and value is {"1":"1", "5":"2", "2":"3", "6":"4"} which is for `PrimaryData` tag.
  </code></pre>
  
  <p>And also for <code>SecondaryData</code> tag in the JSON String- </p>
  
  <pre><code>Here Key of `secondaryData` map is `DC1` and value is {"0":"2", "1":"3", "2":"4", "3":"1"} which is for `SecondaryData` tag.
  Similarly, another Key of `secondaryData` map is `DC2` and value is {"7":"1", "0":"8", "2":"5", "10":"9"} which is for `SecondaryData` tag.
  </code></pre>
  
  <p>And also for <code>MachineMapping</code> tag in the JSON String- </p>
  
  <pre><code>Here Key of `machineMapping` is `DC1` and value is {"3":"dc11115.dc1.domain.com"}, {"2":"dc11114.dc1.domain.com"}, {"1":"dc11113.dc1.domain.com"}, {"4":"dc11116.dc1.domain.com"} which is for MachineMapping tag.
  Similarly another key of `machineMapping` is `DC2` and value is {"3":"dc21147.dc2.domain.com"}, {"2":"dc21146.dc2.domain.com"}, {"1":"dc21145.dc2.domain.com"}, {"4":"dc21148.dc2.domain.com"} which is for `MachineMapping` tag.
  </code></pre>
  
  <p>This is the first time I am working with parsing of this complex JSON String so I started working with GSON as I was finding it pretty easy to work with. This is what I have tried so far and I am able to extract most of the fields but not sure how to map <code>PrimaryData</code> object into a <code>Map</code> and then do the same thing for others?</p>
  
  <pre><code>private static Map&lt;String, Map&lt;String, String&gt;&gt; primaryData = new LinkedHashMap&lt;String, Map&lt;String, String&gt;&gt;();
  private static Map&lt;String, Map&lt;String, String&gt;&gt; secondaryData = new LinkedHashMap&lt;String, Map&lt;String, String&gt;&gt;();
  private static Map&lt;String, Map&lt;String, String&gt;&gt; machineMappedData = new LinkedHashMap&lt;String, Map&lt;String, String&gt;&gt;();
  
  public static void main(String[] args) {
  
      String jsonLine = "my json string";
  
      JsonElement jelement = new JsonParser().parse(jsonLine);
      JsonObject jobject = jelement.getAsJsonObject();
  
      boolean changed = jobject.get("HasChanged").getAsBoolean();
      String timestamp = jobject.get("LastModifiedDate").getAsString();
      String version = jobject.get("Version").getAsString();
  
      System.out.println(changed);
      System.out.println(timestamp);
      System.out.println(version);
  
      if (changed) {
          JsonArray jarray = jobject.getAsJsonArray("DATACENTERS");
          for (int i = 0; i &lt; jarray.size(); i++) {
              Map&lt;String, String&gt; data = new LinkedHashMap&lt;String, String&gt;();
  
              jobject = jarray.get(i).getAsJsonObject();
              String dcName = jobject.get("Name").getAsString();
              int servers = jobject.get("TotalNumberOfServers").getAsInt();
              System.out.println(dcName);
              System.out.println(servers);
  
              if (servers &gt; 0) {
                  JsonArray jarray1 = jobject.getAsJsonArray("PrimaryData");
                  System.out.println(jarray1);
                  for (int j = 0; j &lt; jarray1.size(); j++) {
                      jobject = jarray1.get(j).getAsJsonObject();
                      System.out.println(jobject);
  
                      // not sure how to map the PrimaryData object to a Map here?
  
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>But I have got stuck on it and not sure how do I put the JSON object in a Map here? Can anyone help me with this?</p>