在我的asp.net网页中,我在JavaScriptSerializer类的帮助下准备了JSon字符串。 我在RegisterStartupScript方法的帮助下将Json字符串吐入HTML标记,它看起来像这样,
用于准备json字符串的C#.net代码,
System.Web.Script.Serialization.JavaScriptSerializer jsSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
List<Dictionary<string, string>> glAccts = new List<Dictionary<string, string>>();
Dictionary<string, string> row;
foreach (DataRow dr in _dtProfitCenterRawData.Rows)
{
row = new Dictionary<string, string>();
row.Add(dr[0].ToString(), dr[1].ToString());
glAccts.Add(row);
}
string jsonObj = jsSerializer.Serialize(glAccts);
string script = "var glList = " + jsonObj + ";";
ClientScriptManager cs = Page.ClientScript;
cs.RegisterStartupScript(Page.GetType(), "JSONObj", script, true);
客户端html上的glList变量如下所示,
var glList = [
{ "1110005": "1110005 - X1" },
{ "1110008": "1110008 - X2" },
{ "1110011": "1110011 - X3" },
{ "1110020": "1110020 - X4" }
];
我想将此json字符串绑定到dropdownlist控件。请建议如何执行该操作?我试图执行以下操作来查看对象内的数据,但它没有给我真正的值。它在警报方法中提供[object]。
请建议解决问题..
$.each(glList, function (val, text) {
//$('#mySelect').append(new Option(text, val));
alert(text); alert(val);
});
答案 0 :(得分:2)
在这里试试。如果您遍历glList
,则会获得对象,但不会获取其属性。
function jsonTest() {
$.each(glList, function(index, obj) {
($.each(obj, function (key, value) {
$('#mySelect').append(new Option(key, value));
}));
});
}
答案 1 :(得分:0)
答案 2 :(得分:0)
试
var glList = [
{ 1110005: "1110005 - X1" },
{ 1110008: "1110008 - X2" },
{ 1110011: "1110011 - X3" },
{ 1110020: "1110020 - X4" }
];