title: Correct way for parsing JSON objects containing arrays in Java Servlets (with Gson for example)
body: <p>I know this is a topic much talked about, but I still wasn't able to find a correct and clear answer to my specific problem.</p>
  
  <p>I've got a JSON that looks like this:</p>
  
  <pre><code>var myData = {  "gameID" : gameID,
                  "nrOfPlayers" : 2,
                  "playerUIDs" : [123, 124]
              };
  </code></pre>
  
  <p>The Question I have is exactly what is the correct way (or best way, style wise) to parse this in a Java servlet (using GSON for example)? First I send this JSON to the server using jQuery ajax method like this:</p>
  
  <pre><code>jQuery.ajax({
          url : path,
          data : myData,
          success : successFunction,
          error : function(data) {
              console.log("Error: ", data);
          }  ,
          type : "post",
          timeout : 30000
      });
  </code></pre>
  
  <p>Now in the servlet I've learned that I should have been able to parse that JSON like this:</p>
  
  <pre><code>protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          Gson gson = new Gson();
  
          String gameID = gson.fromJson(request.getParameter("gameID"), String.class);
          String nrOfPlayers = gson.fromJson(request.getParameter("nrOfPlayers"), String.class);
          String[] playerUIDs = gson.fromJson(request.getParameter("playerUIDs"), String[].class);
  
          log.info(gameID);
          log.info(nrOfPlayers);
          log.info(playerUIDs[0] +" "+ playerUIDs[1]);
  }
  </code></pre>
  
  <p><strong>But the playerUIDs variable IS NULL and of course playerUIDs[0] throws an exception!</strong></p>
  
  <p>Digging deeper I found that when looping over the request parameter names, it contained a <strong>parameter named "playerUIDs[]"</strong> with the <strong>value of only 123</strong> (the first int in the array). This was strange cause I didn't seem to be able to access the next values at all.</p>
  
  <p>Then I read that JSON objects should be stringifyed before POST-ing so I added JSON.stringify(myData), but now the request parameter names only <strong>contained ONE name</strong> which was the JSON object itself in a stringified state:</p>
  
  <pre><code>INFO: Parameter name = {"gameID":"b6a51aabb8364b04bce676eafde1bc87","nrOfPlayers":2,"playerUIDs":[123,124]}
  </code></pre>
  
  <p>The only way I seemed to get this to work was by creating a inner class:</p>
  
  <pre><code>class GameStart {
      protected String gameID;
      protected int nrOfPlayers;
      protected int[] playerUIDs;
  }
  </code></pre>
  
  <p>And parsing the JSON from the request parameter name, like this:</p>
  
  <pre><code>protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  
          Gson gson = new Gson();
          Enumeration en = request.getParameterNames();
          GameStart start = null;
          while (en.hasMoreElements()) {
              start = gson.fromJson((String) en.nextElement(), GameStart.class);
          }
          log.info(start.gameID);
          log.info(String.valueOf(start.nrOfPlayers));
          log.info(start.playerUIDs[0] +" "+ start.playerUIDs[1]);
  }
  </code></pre>
  
  <p>Now all values are there, but this seems more like a hack (reading JSON from request parameter name) than an elegant solution, so I thought I'd ask you guys what exactly would be the "correct" way of doing this? Am I missing something obvious?</p>
  
  <p>Thanks in advance!</p>