2014-10-17 95 views
-2

我有被構造爲使遠程JSON文件:在getJSON請求中使用全局變量?

pano100 {"name":"pano100","thumb":"\/pioneer_farm_100\/thumbnail.jpg"}, 
pano101 {"name":"pano100","thumb":"\/pioneer_farm_101\/thumbnail.jpg"} 

凡panoXXX對應於對象的類名。我正在嘗試查找遠程JSON併爲我所在的對象檢索特定類的拇指值。我一直在與代碼的工作如下

var cN = $(this).attr("class"); 
    $.getJSON("custom/parse.php", function(data) { 
     alert(data.cN.thumb); 
    }); 

,我發現了錯誤:未定義是不是(評估「data.cN.thumb」)的對象,我怎麼指導Javascript來對待CN值全局變量而不是文字文本?

謝謝 -Matt

回答

3

使用括號標記:

alert(data[cN].thumb); 
+0

咩,你贏了... – 2014-10-17 19:31:41

+0

你們贏了。 :) – 2014-10-17 19:31:54

+0

非常感謝大家,非常有幫助。 – 2014-10-17 19:39:20