Ebb*_*low 6 .net javascript c# asp.net json
我有以下对象图,我使用Jquery的$ .Ajax()将这个相同的"View"对象以JSON(字符串化)从浏览器发送到ASP.Net上的Page方法.JAvascript反序列化适用于View类中的所有字符串和int,但My List<DataItem>为空.
我尝试过:使用chrome dev工具,我使用了字符串化的JSON,创建了一个单元测试并使用了DataContractJsonSerializer和JavaScriptSerializer.该DataContractJsonSerializer对象正确地反序列化了我的对象图,但JavaScriptSerializer转储了我的列表.如何在页面方法上获得正确的反序列化?
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);
}
});
Run Code Online (Sandbox Code Playgroud)
而不是这个(视图obj作为参数).
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
[WebMethod]
public static string CreateResponseReview(View mqvm)
{
return "Success";
}
Run Code Online (Sandbox Code Playgroud)
我怎么能得到这个?(字符串参数)
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
[WebMethod]
public static string CreateResponseReview(string mqvm)
{
//do manual JSON deserialization here.
return "Success";
}
Run Code Online (Sandbox Code Playgroud)
我的JSON看起来像这样.
{
"Text": "6",
"AnotherText":"wow"
"SomeInt": 5,
"DataItem":[
{
"person":{
"Age":23
}
},
{
"person":{
"Age":42
}
}
]
}
Run Code Online (Sandbox Code Playgroud)
Ebb*_*low 11
我想到了.
我不想使用JavascriptSerializer类,因为它转储了我的嵌套列表,因此我强制它将对象作为字符串传递给我,然后我手动反序列化它.我也一直得到"没有为u0027system.string u0027的类型定义无参数构造函数"
请记住,U0027是一个撇号,因此运行时可能会认为存在名为"System.string"的实际类型而不是System.string.我的问题是 - 我没有正确划分下面名为data2的项目中的参数.我不得不在键和值周围加上"滴答声".
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;
}
Run Code Online (Sandbox Code Playgroud)
尝试使用以下内容:
反序列化代码
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);
Run Code Online (Sandbox Code Playgroud)
类
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;}
}
Run Code Online (Sandbox Code Playgroud)
JSON
{
"text":"some text",
"anotherText":"some more text",
"someInt":1,
"dataItems":
[
{"person":{age:25}},
{"person":{age:20}}
]
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
21847 次 |
| 最近记录: |