title: Creating json object in javascript and sending it to servlet
body: <p>i am creating json object to save data and then sending it to the servlet. But when i try to retrieve the object and display its contents in java servlet, it throws me an error saying " A JSONObject text must begin with '{' at 1 [character 2 line 1]". I do not know how to display the json object in java which is been sent from javascript. Here's what i am doing:</p>
  
  <p><b>Javascript Code:</b></p>
  
  <pre><code>var arrayOfObjects = [];
  arrayOfObjects.push({"divid":imageinc,"xCordinates":Xcord,"yCordinates":Ycord,"Height":canvasWidth,"Width":canvasHeight, "PageNo":pageNum});
  </code></pre>
  
  <p><b>Displaying Javascript contents:</b></p>
  
  <pre><code>for (var i = 0; i &lt; arrayOfObjects.length; i++) {
  var object = arrayOfObjects[i];
  for (var property in object) {
      alert(property + '=' + object[property]);
  }
  }
  </code></pre>
  
  <p><b>Sending object to servlet with jquery Ajax:</b></p>
  
  <pre><code>    var param = 'objarray=' +arrayOfObjects;
  
      $.ajax({
        url: '/ProjectName/finalXmlServGen',
        type: 'POST', 
        dataType: 'json',  
        data: param,
        success: function(result) {
            alert('SUCCESS');
        }
      });
  </code></pre>
  
  <p><b>I get success message once i call the ajax. Now, i am receiving this object in servlet as:</b></p>
  
  <pre><code>String objarray = request.getParameter("objarray").toString();
  try {
          JSONObject jsonObj = new JSONObject(objarray);          
  
          String xmlString= XML.toString(jsonObj);
          System.out.println("JSON to XML: " + xmlString);
      } catch (JSONException e1) {
          // TODO Auto-generated catch block
          e1.printStackTrace();
      }
  </code></pre>
  
  <p>The message i see in my console is the error " A JSONObject text must begin with '{' at 1 [character 2 line 1]". How do i parse the object and form an xml or string?</p>