2012-02-14 32 views
1

有沒有什麼辦法可以訪問jQuery.getJSON數據與網址作爲對象? 下面是我從getJSON調用中得到的回覆:以URL作爲對象訪問JQuery.getJSON數據

{"http://example.com/example":"value", 
"http://example2.com/example":"value2"} 
+2

你到底想幹什麼?這是你從getJSON調用中得到的迴應嗎? – ShankarSangoli 2012-02-14 18:22:37

回答

1

是的,你可以。

例如:

var h = { 
"http://yahoo.com" : "good", 
"http://google.com" : "better" 
}; 
for(var key in h) { 
alert(key+" => "+h[key]); 
} 
1

首先,你必須在所有的對象:

var h = { 
"http://yahoo.com" : "good" 
}; 
alert(h['http://yahoo.com']); 

在你的情況,你可能會使用for..in語句一樣要通過項目環在問題中是無效的,你不能在對象定義裏面有分號。您應該使用逗號來分隔對象屬性。

var obj = {"http://example.com/example":"value", 
"http://example2.com/example":"value2"}; 

您可以像這樣訪問上述對象的屬性。

alert(obj["http://example.com/example"]); 

http://jsfiddle.net/EW9D3/