0

I'm a JSON/Javascript newbie and have a problem of this nature.

within my php script I'm passing a php array to javascript as follows:

echo '<img id="Next" src="misc/arrow_right.png" onclick="imageSlider(' . json_encode($images) . ')" >';

in my javascript:

function imageSlider(imagesArray) {
alert(imagesArray); 
}

the alert above prints an output of the form object Object

The trouble is I do not know how best to 'decompose' or break down this object so that I can extract its the data that I originally json_encoded.The structure of the original array is a simple numeric indexed array with strings i.e ["image1", "image2",...] etc

I have also tried alert(JSON.stringify(imagesArray)) and I get this string:

{"1":"images1.jpg","2":"images2.jpg","3":"images3.jpg","4":"images4.jpg","5":"images5.jpg"}

Either way I am not sure how best to get my images names and array index in javascript. e.g extract 1 and images1.jpg from the object or if there is a way to convert the 'stringified' object into a true javascipt array...? Thanks

1
  • it alreaady is a havascript object. json = javascript obj. notation. try imagesArray[1] to see that it actually works. Commented Mar 4, 2014 at 22:12

3 Answers 3

0

You can use famous json2.js to parse json string into object in Javascript. After including json2.js, you can use:

imageArray = JSON.parse(encodedJson);

Bu i think in your example it is already a true javascript object if you have a json encoded object. You can use

console.log(imageArray);

to see contents of imageArray. And you can access the properties of your object like:

alert(imageArray[1]) //this will alert "image1.jpg"

But i want you to know that imageArray is not an array, it is a javascript object. And you cannot use dot notation to reach javascript object's values if the key is a number (like in your example). So you can not use imageArray.1 to reach "image1.jpg", you should use square brackets if your keys are numbers like the code above. If you want to use dot notation, you should make your keys strings such as:

imageArray = { "i1": "image1.jpg", "i2": "image2.jpg" } //this way you can use imageArray.i1 to get "image1.jpg"
Sign up to request clarification or add additional context in comments.

2 Comments

Awesome 'floppy disk'. Thanks a lot. You spotted my other ignorance on numeric keys, I had initially tried imageArray.1 without joy. Thanks very much.
:) My pleasure to help
0

You were close.

function imageSlider(imagesObject) {

    //access it like an Array (note the quotes around 1), no other changes needed.
    imagesObject["1"];

}

Comments

-1
var imgs=eval('('+imagesArray+')');

Then you can access imgs object.

Keep in mind that this is not an indexed array any more, but a key-value store:

for (key in imgs){
  var img=imgs[key];
  console.log(img);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.