How to properly deserialize a JSON string into a class containing a nested list of another class

I have the following graph of objects, and I use JQuery $ .Ajax () to send this identical View object in JSON (gated) from the browser to the page method on ASP.Net. JAvascript deserialization works for all rows and int in the View class, but My List<DataItem> empty.

What I tried: using the chrome dev tools, I took a lowercase JSON, created a unit test and used both DataContractJsonSerializer and JavaScriptSerializer . The DataContractJsonSerializer object has deserialized my object graph correctly, but the JavaScriptSerializer dumped my list. How can I get the correct deserialization of 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 (representing obj as a parameter).

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

how can i get this? (string parameter)

  [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 } } ] } 
+6
source share
2 answers

I get it.

I did not want to use the JavascriptSerializer class because it dumped my nested list, so I made it pass me the object as a string, and then I manually deserialized it. I also kept getting "without a parametric constructor defined for type u0027system.string u0027"

Remember that U0027 is an apostrophe, so the runtime might think that there is an actual type called "System.string" and not System.string. My problem was that I did not correctly limit the parameters in the element below called data2. I had to put ticks around the key and 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; } 
+11
source

Try using the following:

Deserialization Code

 string json = "{\"text\":\"some text\",\"anotherText\":\"some more text\", \"someInt\":1, \"dataItems\":[{\"person\":{age:25}},{\"person\":{age:20}}]}"; JavaScriptSerializer serializer = new JavaScriptSerializer(); View view = serializer.Deserialize<View>(json); 

Classes

 public class View { public string Text { get; set; } public string AnotherText { get; set; } public int SomeInt { get; set; } public List<DataItem> DataItems { get; set; } } public class DataItem { public Person person { get; set; } } public class Person { public int Age {get;set;} } 

Json

 { "text":"some text", "anotherText":"some more text", "someInt":1, "dataItems": [ {"person":{age:25}}, {"person":{age:20}} ] } 
+5
source

Source: https://habr.com/ru/post/898099/


All Articles