is there a possibility to convert a json-object to a array or something?
i like to have a function, which is retrieving data from a sqldatabase via json:
jQuery.getJSON(“file.php”, function(data) {
…
});
this data should be stored in an array, so that i can use it on several positions in the website
If your
file.phpscript returns the appropriate data type for it’s response, then thedataargument will already contained parsed JSON (e.g. in live javascript variable form).If you don’t like the form that the data is in and you want to convert it to an array from something else, you would have to show us what format it’s in so we can advise on code to convert it to some other form.
If you just want to store the data in a variable so you can use it elsewhere, then you can do that by storing it in a global variable:
The data is now in a global variable named fileData that you can use anywhere on your page. Keep in mind that a getJSON call is asychronous so it might take a little while to complete and the data won’t be available in your variable until the getJSON callback is called.
If you were calling this multiple times and wanted to collect each response, you could collect them into an array like this: