Monday, May 20, 2024
Homepage · c#
 Popular · Latest · Hot · Upcoming
74
rated 0 times [  77] [ 3]  / answers: 1 / hits: 24012  / 13 Years ago, mon, september 26, 2011, 12:00:00

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 List<DataItem> is empty.



What I tried: Using chrome dev tools, I took the stringified JSON, created a unit test and used both the DataContractJsonSerializer and the JavaScriptSerializer. The DataContractJsonSerializer object deserialized my object graph correctly but the JavaScriptSerializer dumped my List. How can I get 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);
}
});


Instead of this (the view obj as a param).



    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
[WebMethod]
public static string CreateResponseReview(View mqvm)
{
return Success;
}


how can I get this? (the string param)



    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
[WebMethod]
public static string CreateResponseReview(string mqvm)
{
//do manual JSON deserialization here.

return Success;
}


My JSON looks like this.



    {
Text: 6,
AnotherText:wow
SomeInt: 5,
DataItem:[
{
person:{
Age:23
}
},
{
person:{
Age:42
}
}
]
}

More From » c#

 Answers
19

I figured it out.



I didn't want to use the JavascriptSerializer class because it dumped my nested list so I forced it to pass me the object as a string and then I manually deserialized it. I also kept getting no parameterless constructor defined for type of u0027system.string u0027



Remember that U0027 is an apostrophe so the runtime might be thinking that there is an actual type named System.string and not System.string. My problem was - I wasn't correctly delimiting the parameters in the item below called data2. I had to put ' ticks ' around the key and the value.



function requestHtmlFromServer(mqvmJSON) {
var mqvmstring = JSON.stringify(mqvmJSON);
var data2 = {'mqvm':' + mqvmstring + ' }; \<--the problem
$.ajax({
type: POST,
dataType: json,
contentType: application/json; charset=utf-8,
data: data2,
url: MedicalInformation.aspx/CreateResponseReview,
success: function(data) {
alert(data.d);
},
error: function(jqXHR, textStatus, errorThrown) {
alert(jqXHR.responseText + ' ' + errorThrown);
}
});
}


[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
[WebMethod]
public static string CreateResponseReview(string mqvm)
{
string noNewLines = mqvm.Replace(n, );
View viewModel = ToObjectFromJSON<View>(noNewLines);
//do my other stuff here

return Success;
}
public static T ToObjectFromJSON<T>(string jsonString)
{
var serializer = new DataContractJsonSerializer(typeof(T));
var memoryStream = new MemoryStream(Encoding.Unicode.GetBytes(jsonString));
var newObject = (T)serializer.ReadObject(memoryStream);
memoryStream.Close();
return newObject;
}

[#89912] Sunday, September 25, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jennie

Total Points: 593
Total Questions: 102
Total Answers: 106

Location: Federated States of Micronesia
Member since Fri, Sep 16, 2022
2 Years ago
jennie questions
;