如何使用jQuery动态访问JSON密钥

时间:2010-12-17 05:33:09

标签: jquery json

我有以下内容:

var parsed_contacts = jQuery.parseJSON(conts);
$('#contacts > li > a').click(function(event){
          target_id = event.target.id; //yields a Google App Engine entity key bound to a <a> id property. (e.g "agdldHJlYnVncg0LEgdDb250YWN0GFgM", with quotes)
          console.log(parsed_contacts[target_id]); //Yields undefined
});

这是我从console.log(parsed_contacts)得到的:

alt text Click for bigger image

我该怎么办? 提前谢谢!

2 个答案:

答案 0 :(得分:2)

从您的输出中,您似乎需要parsed_contacts[0][target_id]

答案 1 :(得分:1)

如果您的字符串包含引号,则可能需要在将其传递给console.log之前将其删除:

target_id = target_id.replace(/['"]/g,'');
相关问题