我有这个json:[{"Id":"1", "ZipCode":"00691","City":"Adjuntas","Municipality":"Adjuntas"},{"Id":"2", "ZipCode":"00603","City":"Aguadilla","Municipality":"Aguadilla"},{"Id":"3", "ZipCode":"00674","City":"Manati","Municipality":"Manati"}]
我的问题是使用javascript' json.parse()对此进行反序列化。
这是我尝试过的
function OnZipCodeBlur()
{
var hfZipCodesValue = document.getElementById("<%=hfZipCodesMunicipalitiesCities.ClientID%>").value;
var zipCode = document.getElementById("<%=tbZipCode.ClientID%>").value;
alert("serialization: " + hfZipCodesValue);
var zipCodeDeserialized = JSON.parse(hfZipCodesValue);
var d = "";
for (var i = 0; i < zipCodeDeserialized.length; i++)
{
if (zipCodeDeserialized[i].ZipCode == zipCode)
{
document.getElementById('<%=tbCity.ClientID%>').value = zipCodeDeserialized[i].City;
document.getElementById('<%=tbMunicipality.ClientID%>').value = zipCodeDeserialized[i].Municipality;
break;
}
d += zipCodeDeserialized[i];
}
alert("deserialization : " + d);
}
hfZipCodesValue
- 保存序列化数据
ZipCode
- 持有邮政编码。
正如您所看到的,我有两个警报显示序列化数据并反序列化。第一个警报正确显示,第二个警报只返回[object Object],[object Object],[object Object]。起初我认为我必须选择一个索引,而不是像我现在所做的那样显示整个数组,但是,当我意识到它是一个字符数组时。换句话说,d[0] == "["
和d[1] == 'o'
。没关系if中的if,它永远不会导致对象结果。
任何建议都将不胜感激。