-1

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

Benjamin Birk
  • 79
  • 2
  • 8
  • Why do you want an array (with positional access only), when you could have a Javascript object (with named access)? – Philipp Reichart Sep 22 '11 at 22:13
  • http://stackoverflow.com/questions/5618548/convert-json-array-to-javascript-array – Cory Danielson Sep 22 '11 at 22:16
  • Do you just want to wrap the JSON object in an array? We need more details, give us an example to help. – adamjmarkham Sep 22 '11 at 22:17
  • 1
    First, there are no JSON objects. There are JSON strings and JavaScript objects. A JSON string can be easily converted to a JavaScript object. You will be dealing with a JavaScript object as that is what jQuery passes to the `success` callback from `$.getJSON()`. Second, please clarify what you are trying to do. Why do you need an Array? You can use the same data in several positions on a web page without using an Array. What does your data look like, and what do you want your array to look like? – gilly3 Sep 22 '11 at 22:19
  • 1
    Do you want to have an array with one json object in it? Could you give one example of the intended use? –  Sep 22 '11 at 22:19

2 Answers2

0

If you just want to wrap the json object in an existing array, then use the push() method of the array in question, passing the object as the parameter.

twilson
  • 2,062
  • 14
  • 19
0

If your file.php script returns the appropriate data type for it's response, then the data argument 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:

var fileData;
jQuery.getJSON("file.php", function(data) {
     fileData = data;
     // call any functions here that might want to process this data as soon as it's ready
});

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:

var fileData = [];    // declare empty array
jQuery.getJSON("file.php", function(data) {
     fileData.push(data);  // add onto the end of the array
     // call any functions here that might want to process this data as soon as it's ready
});
jfriend00
  • 683,504
  • 96
  • 985
  • 979