在后面的 c# 代码中,我正在创建一个对象并尝试在前端 javascript 中访问它。但它总是得到实例化值而不是对象值;请看下面的代码:
public class Product
{
public String[] Sizes { get; set; }
public decimal Price { get; set; }
public DateTime Expiry { get; set; }
public string Name { get; set; }
}
public partial class _Default : System.Web.UI.Page
{
public string sJSON = "shuvra";
public JavaScriptSerializer javaSerial = new JavaScriptSerializer();
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Btn2_Click(object sender, EventArgs e)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
Product product = new Product
{
Name = "Apple",
Expiry = new DateTime(2008, 12, 28),
Price = 3.99M,
Sizes = new[] { "Small", "Medium", "Large" }
};
var oSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
sJSON = oSerializer.Serialize(product);
JavaScriptSerializer ser = new JavaScriptSerializer();
//ser.Serialize(arrayMarker, jasonSerialize);
sb.AppendLine("markicons();");
ScriptManager.RegisterStartupScript(up_IFASelected, typeof(UpdatePanel), this.ClientID + "_LoadMap", sb.ToString(), true);
}
}
javascript代码:
function markicons() {
initialize();
var a = JSON.parse('<%= this.javaSerial.Serialize(this.arrayMarker) %>');
var b = eval('<%=this.sJSON%>');
}
值“b”始终是 shuvra,但不是分配的值。我该如何解决?