title: ASP.NET MVC2 Reading JSON data with controller
body: <p>I have problems to receive json values from my javascript/jQuery request to my controller.</p>
  
  <p>MyClass looks like the following:</p>
  
  <pre><code>function MyClass() {
      this.MyString = null;
      this.MyInt = null;
      this.Subs = null;
  }
  </code></pre>
  
  <p>My Request looks like the following:</p>
  
  <pre><code>var testData1 = new MyClass();
  testData1.MyInt = 1234;
  testData1.MyString = "abcDEF";
  testData1.Subs = new Array();
  var testData2 = new MyClass();
  testData2.MyInt = 5678;
  testData2.MyString = "GHIjkl";
  testData1.Subs.push(testData2);
  var jsonData = JSON.stringify(testData1);
  var self = this;
  $.ajax({
      url: '/Home/Request',
      type: 'POST',
      dataType: 'json',
      data: jsonData,
      contentType: 'application/json; charset=utf-8',
      success: function (x) {
          self.ParseResult(x);
      }
  });
  </code></pre>
  
  <p>Now I have a controller:</p>
  
  <pre><code>public JsonResult Request(MyClass myObj)
  {
      var answer = ...
      return Json(answer, JsonRequestBehavior.DenyGet);
  }
  </code></pre>
  
  <p>With the following class:</p>
  
  <pre><code>public class  MyClass
  {
      public string MyString { get; set; }
      public int MyInt { get; set; }
      public List&lt;MyClass&gt; Subs { get; set; }
  }
  </code></pre>
  
  <p>All names in jsonData are exactly the same like in my class "MyClass". But there are no values in myObj.</p>
  
  <p>Where is the Problem. Is there anything I can do to get this mapping working correctly?</p>
  
  <p>Thank you very much in advance,</p>
  
  <p>Chris</p>
  
  <p>UPDATE:</p>
  
  <p>Thank you for your repley. I have used the JavascriptSerializer. But I have the problem, that myString is null:</p>
  
  <pre><code>public JsonResult Data(string myString)
  {
      JavaScriptSerializer serializer = new JavaScriptSerializer();
      var data = serializer.Deserialize&lt;MyClass&gt;(myString);
  var answer = ...
      return Json(answer, JsonRequestBehavior.DenyGet);
  }
  </code></pre>
  
  <p>Where is the value? Should I take the value from the request-data?</p>
  
  <p>@Dave Ward</p>
  
  <p>You second solution is working. But I have a problem with the subs.</p>
  
  <pre><code>var testData1 = new MyClass();
  testData1.MyInt = 1234;
  testData1.MyString = "abcDEF";
  testData1.Subs = new Array();
  for (var i = 0; i &lt; 10; i++) {
      var testData2 = new MyClass();
      testData2.MyInt = i;
      testData2.MyString = "abcDEF";
      testData1.Subs.push(testData2);
  }
  </code></pre>
  
  <p>I get 10 Subs in my controller, but all are empty. What can I do?</p>
  
  <p>@Dave Ward, @ALL</p>
  
  <p>Using the traditional settings my 10 Subs are bot empty, they are not there. Subs count is 0 (not NULL).
  I tried to change the Subs Type from List to IEnumerable but that didn't help.
  Do you know anything else I can do to get Subs filled in my controller?</p>
  
  <p>Ok, thank you Dave Ward, I will use the JSON method.</p>
  
  <p>For somebody else having the same problem this controller code could be from help:</p>
  
  <pre><code>MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(myString));
  var serializer = new DataContractJsonSerializer(typeof(MyClass));
  MyClass se = serializer.ReadObject(ms) as MyClass;
  ms.Close();
  </code></pre>