json to javascript

Thanks for your continuous help from all the yii members.

Here is the json output from one of my controllers




[{"attributes":{"item_name":"Ford Escape","city":"Frisco"},"relations":[]},{"attributes":{"item_name":"house","city":"Dallas"},"relations":[]},{"attributes":{"item_name":"Car","city":"Plano"},"relations":[]}] 



Here is the javascript code




function generateListElement(marker, attributes){

var ul = document.getElementById('sideContainer');

var li = document.createElement('li');

var aSel = document.createElement('a');

aSel.href = 'javascript:void(0);';

aSel.innerHTML = attributes.item_name;

aSel.onclick = function(){google.maps.event.trigger(marker, 'click')};

li.appendChild(aSel);

ul.appendChild(li);

}



My markers on the map works fine but on the sidebar <ul id="sideContainer" style></ul> the value that is being outputed is undefined and when I click on it shows the markers fine the only thing I need it the name of the it from the json array. Thanks for any help as soon as possible.

Cause you hava an attributes "json array" (json.org) you cannot simply use attributes.item_name

For me it looks like that you have to loop over the json attributes array for creating the li-tags for each element (in your example 3)

For testing you can try




attributes[0].item_name; // use the first attributes element in the json array => item_name = Ford Escape



Thanks kokomo for reminding me about the loop. it’s now working