Passing json data from api variable to javascript

I am new to json / javascript, so please bear with me. I get json data from api url that loads sth like

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

I need this as a javascript variable in 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); }); 
+4
source share
1 answer

Put the JSON string of var jsonString variable var jsonString

then ...

 var myJsonObject = JSON.parse(jsonString); 
+3
source

Source: https://habr.com/ru/post/1447492/


All Articles