我正在使用本教程来序列化 C# 字典。C# 字典被序列化为字符串。这些@Html.Raw(new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(ElementDivIDs))
作品就像一个魅力。这是我得到的输出:
var jsonString = {"9":["ele9-tabs-attr9","ele9-tabs-attr48"],"10":["ele10-tabs-attr10"],"11":["ele11-tabs-attr11"],"12":["ele12-tabs-attr12","ele12-tabs-attr49"],"13":["ele13-tabs-attr13"],"14":["ele14-tabs-attr14"]}
我想将其转换为 Javascript 关联数组。但是对jquery.parseJSON的调用返回 NULL。
var dictionaryOfOtherDivs = jQuery.parseJSON( jsonString );
dictionaryOfOtherDivs
在此之后为空。
这是我的代码:
<script type="text/javascript">
$(document).ready(function () {
var jsonString = @Html.Raw(new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(ElementDivIDs))
console.log(jsonString);
var dictionaryOfOtherDivs = jQuery.parseJSON( jsonString );
for(var dictKey in dictionaryOfOtherDivs)
{
console.log("key = " + dictKey + ", value = " + dictionaryOfOtherDivs[dictKey]);
}
//Do some more things
});
</script>