|
5金钱
请教一个ASP.NET方面JSON数组转化的问题。
这是前台源码
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="Scripts/jquery-1.4.1-vsdoc.js" type="text/javascript"></script>
<script language="javascript" type="text/javascript">
$(function () {
//获取Stu对象并初始化,存入数组
var stuList = [];
var model = new Stu();
model.Name = "Maigc";
model.Birthday = "2014-09-12";
stuList.push(model);
var str = JSON.stringify(stuList);
$("#" + '<%=HFStu.ClientID %>').val(str);
});
function Stu() {
this.Name = "";
this.Birthday = "";
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:HiddenField ID="HFStu" runat="server" />
<asp:Button ID="btnTransfer" runat="server" Text="JSON转化测试" />
</form>
</body>
</html>
---这是后台源码
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Json;
namespace WebApplication1
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e) { }
protected void btnTransfer_Click(object sender, EventArgs e)
{
List<Stu> RectifyUserList = ParseFormJson<List<Stu>>(HFStu.Value);
}
public static T ParseFormJson<T>(string jsonStr)
{
T obj = Activator.CreateInstance<T>();
using (MemoryStream ms = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(jsonStr)))
{
DataContractJsonSerializer dcj = new DataContractJsonSerializer(typeof(T));
return (T)dcj.ReadObject(ms);
}
}
public class Stu
{
public string Name { get; set; }
public DateTime Birthday { get; set; }
}
}
}
点击转化测试时提示“DateTime 内容“2014-09-12”未按 JSON 的要求以“\/Date(”开始并以“)\/”结束。”
请协助调查该如何解决,谢谢!
|
|