尝试访问json对象中的元素时未定义
问题描述:
第一个alert
如何显示Dublin
但第二个是undefined
?尝试访问json对象中的元素时未定义
http://jsfiddle.net/pzdx6L5a/1/
function getRenderParamValue(query){
customContext = JSON.parse("{\"test\":true,\"geo\":\"\",\"city\":\"Dublin\",\"categoryId\":\"\",\"categoryName\":\"\",\"productId\":\"\"}");
alert(customContext.city);
return customContext.query;
}
alert('city: ' + getRenderParamValue('city'));
答
它寻找一个属性 '查询',这不存在。 使用[]
而不是.
:
return customContext[query];
这应该是'返回customContext [查询]'。 – Barmar 2014-09-04 22:56:22