title: JSON nested object post to node.js server?
body: <p>I want to POST an Object via JSON to node.js server.</p>
  
  <p>The Object structure is nested, and never succeeded to receive and parse correctly on node.js server site.</p>
  
  <hr>
  
  <p>EDIT2</p>
  
  <p>I found a solution: see the answer section...</p>
  
  <hr>
  
  <p>EDIT</p>
  
  <p>I found</p>
  
  <pre><code>     console.log(body);
  </code></pre>
  
  <p>itself output</p>
  
  <p>val1=hello&amp;val2%5Bval3%5D=world</p>
  
  <p>//= {"val1":"hello","val2[val3]":"world"}</p>
  
  <p>weired JSON way</p>
  
  <hr>
  
  <p>client.js</p>
  
  <pre><code>    var data ={val1:"hello",val2:{val3:"world"}};
      console.log(data); // -&gt; *1
      $.ajax({
          url:"/",
          type:"POST",
          dataType: 'json',
          data:data,
          success:function (res)
          {
              resHandler(res);
          }
      });
  </code></pre>
  
  <hr>
  
  <p>*1 ChromeDevelopersTool</p>
  
  <p>Object</p>
  
  <p>val1: "hello"</p>
  
  <p>val2: Object</p>
  
  <p>val3: "world"</p>
  
  <hr>
  
  <p>server.js</p>
  
  <pre><code>var onreq = function (req, res)
  {
      if(req.method == 'POST')
      {
          var body = '';
          req.on('data', function (data)
          {
              body += data;
          });
          req.on('end', function ()
          {
              var json = qs.parse(body);
              console.log(json.val1); //hello
              console.log(json.val2); //undefined
              console.log(json.val3); //undefined
  
              console.log(JSON.stringify(json)); 
              //{"val1":"hello","val2[val3]":"world"}
          });
      }
  </code></pre>
  
  <hr>
  
  <p>I understand </p>
  
  <p>val2[val3]
  is
  val2.val3</p>
  
  <p>However,</p>
  
  <hr>
  
  <p>Problem 1</p>
  
  <p>JSON.stringify prints out</p>
  
  <p>{"val1":"hello","val2[val3]":"world"}</p>
  
  <p>not</p>
  
  <p>{val1:"hello",val2:{val3:"world"}}</p>
  
  <p>It's ugly, and I don't know why it's like that.</p>
  
  <hr>
  
  <p>Problem 2</p>
  
  <p>I can never get {val3:"world"}</p>
  
  <p>console.log(json.val3); //undefined</p>
  
  <hr>
  
  <p>Anyone can explain, and how can I POST a nested JSON to node.js server?</p>
  
  <p>Thanks.</p>