简体   繁体   中英

Passing json data from an api to a javascript variable

Am a newbie in json/javascript so please bear with me. Am getting json data from an api url that loads sth like

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

i need it as a javascript variable on a script file

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

Ave tried

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

Put your JSON string a variable var jsonString

then...

var myJsonObject = JSON.parse(jsonString);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM