title: How can I deserialize in the website the string contained in the result.d of an ajax call?
body: <p>This is a simplified flow, but it will be easier to explain:
  I have an application which calls an webmethod using JQuery, it gets the returning value and stores in a hidden field so this can be accessed by the code behind once a postback is made.</p>
  
  <p>I have the following code:</p>
  
  <pre><code>$.ajax({
          type: 'POST',
          contentType: 'application/json; charset=utf-8',
          data: JSON.stringify({ contacts: _contacts }),
          dataType: 'json',
          url: '/MyPage.aspx/MyMethod',
          success: function (result) {
              hdn.val(result.d);
          }
      });
  </code></pre>
  
  <p>In my code behind I try to get this information and parse it:</p>
  
  <pre><code>    protected void btnRegister_Click(object sender, EventArgs e)
  {
  ContactInfo[] importedContacts = new JavaScriptSerializer().Deserialize&lt;ContactInfo[]&gt;(this.hdContacts.Value);
  }
  </code></pre>
  
  <p>But I get an exception saying:</p>
  
  <blockquote>
    <p>Value cannot be null. Parameter name: type</p>
  </blockquote>
  
  <p>I figured out the the problem is this _Type parameter, which doesnt exist in my ContactInfo object.
  I am wondering how I can deserialize this information. This should be easily deserializable because it comes directly from my ajax call.</p>
  
  <p>Edit: about the "type" parameter: it is added automatically by the .NET Framework in order to serialize the return object for the WebMethod MyPage.aspx/MyMethod. I imagined that if the framework adds this information when serializing the data, I could use the same serializer to deserialize it....</p>
  
  <p>In the hdnValue I have the following string:</p>
  
  <blockquote>
    <p>[{"__type":"Un.Socialize.ContactInfo","ID":"123","FirstName":"First","LastName":"Last","FullName":"First Last","EMail":"email@email.com","Nickname":"Nick","Picture":"Pic.jpg","AlreadyExists":false}]</p>
  </blockquote>
  
  <p>If I (during debug) remove the "__type:..." parameter, it works. </p>
  
  <p>Reading <a href="http://encosia.com/asp-net-web-services-mistake-manual-json-serialization/" rel="nofollow">this article</a> I noticed that the problem was not on the webmetho response, but on my deserialization. I shouldn't try to do that.
  Because I have some old code there, I will have to find an workaround, maybe storing the values in the hdnField and building the objects in my server side.</p>