2012-11-21 66 views
4

是json/javascript中的新手,請耐心等待。我從一個加載API URL獲取JSON數據某物像將json數據從api傳遞給javascript變量

{ 
"employees": [ 
{ "firstName":"John" , "lastName":"Doe" }, 
{ "firstName":"Anna" , "lastName":"Smith" }, 
{ "firstName":"Peter" , "lastName":"Jones" } 
] 

我需要它作爲一個腳本文件

var employess={ 
"employees": [ 
{ "firstName":"John" , "lastName":"Doe" }, 
{ "firstName":"Anna" , "lastName":"Smith" }, 
{ "firstName":"Peter" , "lastName":"Jones" } 
] 
} 

大道試圖

$.getJSON(myUrl",function(data){ $("#selector").data("JSONP",data); alert(data); }); 
+3

JSON是一種字符串格式,而不是Javascript對象。 'var object = JSON.parse(jsonString)'https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/JSON/parse – jrajav

+2

你有什麼問題? – SLaks

+1

'employees = data'? – sje397

回答

3

把你的JSON字符串javascript變量變量var jsonString

然後...

var myJsonObject = JSON.parse(jsonString);