title: Create json string from js Map and String
body: <p>My REST controller expects request input of the following format, which it successfully converts to a Java object containing a Map and a String as parameters:</p>
  
  <pre><code>{ 
  "myMap" : { 
      "key1": "value1", 
      "key2": "value2", 
      "key3": "value3"},
  "myString": "string value"
  }
  </code></pre>
  
  <p>I am getting my data from an html form like so:</p>
  
  <pre><code>var myMap = new Map();
  var String = document.getElementById('String').value;
  for (var i = 0 ; i&lt;anArray.length ; i++){
      var input = document.getElementsByClassName('input_' + (i+1));
      for (var j = 0 ; j&lt;3 ; j++){
          if (input[j].checked){
              myMap.set(input[j].name, input[j].id);
          }
      }
  }
  </code></pre>
  
  <p>Basically, this code boils down to:</p>
  
  <pre><code>var myMap = new Map();
  myMap.set("key1", "value1");
  myMap.set("key2", "value2");
  myMap.set("key3", "value3");
  </code></pre>
  
  <p>This results in a map containing {key1 => value1, key2 => value2, etc} and a String. I have been trying to turn this into a json string like so, but it doesn't seem to work:</p>
  
  <pre><code>var myJson = {};
  myJson.myMap = myMap;
  myJson.myString = myString;
  var json = JSON.stringify(myJson);
  </code></pre>
  
  <p>However, I am ending up with the following string: `{"myMap":{},"String":"myString"}' . So I probably have to do something different to stringify a map, but nothing I try is working. </p>
  
  <p>Can anyone help me out?</p>