I am currently working on a page that will read in XML files that are in a directory (dynamically) and then send them back to the client via JSON. However I’m not sure how to reference the dynamic elements of the JSON array that is returned. EG.
$.getJSON('getfilenames.php', function(data) {
for(var i in data)
{
var temp3 = data[i].name;(<-- if I dont know what .name was)
var temp2 = temp3.replace(".xml","");
temp = temp +'<option value="'+temp3+'">' +temp2 +'</option>';
}
temp = temp +'</select>';
$("#div2").html(temp);
}
If I didn’t already know what the .name element within data was, how could I :
A: retrieve a list of them for referencing, or B: access the data to do it myself.
I am also curious as to how I go about getting elements from within elements. EG:
{"contact":{"@attributes":{"id":"1"},"name":"John Doe","phone":"123-456-7890","address":"\n 123 JFKStreet\n "}}
How would I get the name attribute out.
You just need to iterate over the properties of the returned object..
You can go as deep in an object with multiple
.notation..from your last example (assuming the variable holding this object is
data)