I have a scenario where I am sending a JSON stringified "View" object from the browser to an ASP.Net Page Method using Jquery's $.Ajax(). The issue I am facing is that while the Javascript deserialization is successful for all the strings and integers in the View class, the List<DataItem> remains empty.
To troubleshoot, I used the stringified JSON in the Chrome dev tools to create a unit test. I tried deserializing it using both the DataContractJsonSerializer
and the JavaScriptSerializer
. Surprisingly, the DataContractJsonSerializer
correctly deserialized my object graph while the JavaScriptSerializer
did not fetch the List data. How can I ensure the correct deserialization on my page method?
public class View
{
public string Text { get; set; }
public string AnotherText { get; set; }
public Int SomeInt { get; set; }
public List<DataItem> { 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);
}
});
If I want to receive the data as a string parameter instead of the view object:
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
[WebMethod]
public static string CreateResponseReview(string mqvm)
{
//do manual JSON deserialization here.
return "Success";
}
Here is an example of how my JSON object looks:
{
"Text": "6",
"AnotherText":"wow"
"SomeInt": 5,
"DataItem":[
{
"person":{
"Age":23
}
},
{
"person":{
"Age":42
}
}
]
}