2014-09-04 67 views
0

第一个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')); 
+0

这应该是'返回customContext [查询]'。 – Barmar 2014-09-04 22:56:22

回答

0

它寻找一个属性 '查询',这不存在。 使用[]而不是.

return customContext[query];