Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to correctly deserialize a JSON string into the class that contains a nested List of another class
    primarykey
    data
    text
    <p>I have the following object graph and I'm using Jquery's $.Ajax() to send this identical "View" object in JSON (stringified) from the browser to a Page Method on ASP.Net. The JAvascript deserialization works for all of the strings and int's in the View class but My <code>List&lt;DataItem&gt;</code> is empty. </p> <p>What I tried: Using chrome dev tools, I took the stringified JSON, created a unit test and used both the <code>DataContractJsonSerializer</code> and the <code>JavaScriptSerializer</code>. The <code>DataContractJsonSerializer</code> object deserialized my object graph correctly but the <code>JavaScriptSerializer</code> dumped my List. How can I get the correct deserialization on my page method ?</p> <pre><code>public class View { public string Text { get; set; } public string AnotherText { get; set; } public Int SomeInt { get; set; } public List&lt;DataItem&gt; { get; set; } } public class DataItem { public Person person {get;set} } public class Person { public int Age {get;set} } var dataa = {mqvm: mqvmJSON }; $.ajax({ type: "POST", dataType: "json", contentType: "application/json; charset=utf-8", data: JSON.stringify( dataa ), url: "GoHere.aspx/WebMethodName", success: function(data) { alert(data.d); }, error: function(jqXHR, textStatus, errorThrown) { alert(jqXHR.responseText + ' ' + errorThrown); } }); </code></pre> <p>Instead of this (the view obj as a param).</p> <pre><code> [ScriptMethod(ResponseFormat = ResponseFormat.Json)] [WebMethod] public static string CreateResponseReview(View mqvm) { return "Success"; } </code></pre> <p>how can I get this? (the string param)</p> <pre><code> [ScriptMethod(ResponseFormat = ResponseFormat.Json)] [WebMethod] public static string CreateResponseReview(string mqvm) { //do manual JSON deserialization here. return "Success"; } </code></pre> <p>My JSON looks like this.</p> <pre><code> { "Text": "6", "AnotherText":"wow" "SomeInt": 5, "DataItem":[ { "person":{ "Age":23 } }, { "person":{ "Age":42 } } ] } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload