Ok I'm realtively new to PHP, so hang in there with me, trying hard to learn this stuff. Anyways, so pretty much I have a php page that get's the JSON data contents and echo's it on the page just fine. So my problem is that my JSON is notated multidimensionally like so:
{"product":[{"brandId":"632","brandName":"Sam Edelman","productId":"7515478","productName":"Gigi","styles":[{"price":"$64.95","styleId":"1788226","imageUrl":"http:\/\/www.zappos.com\/images\/z\/1\/7\/8\/8\/2\/2\/1788226-p-DETAILED.jpg","originalPrice":"$64.95","productUrl":"http:\/\/www.zappos.com\/product\/7515478\/color\/25","percentOff":"0%","color":"Almond"},
So how would I access say the percentOff in the first element? Here's my javascript function that I use to ideally grab elements and make decisions based around grabbing the "percentOff" element.
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function ajax_get_json(){
var results = document.getElementById("results");
var hr = new XMLHttpRequest();
hr.open("GET", "my_json_list.php", true);
hr.setRequestHeader("Content-type", "application/json", true);
hr.onereadystatechange == function() {
if(hr.readyState == 4 && hr.status == 200){
var data = JSON.parse(hr.responseText);
results.innerHTML = "";
for(var obj in data){
results.innerHTML += data[obj].styles[obj].percentOff+""; //Or like this?
results.innerHTML += data[0].styles[obj].percentOff+""; //Would it look something like this?
}
}
}
hr.send(null);
results.innerHTML = "requesting...";
}
</script>
</head>
<body>
<div id="results"></div>
<script type="text/javascript">ajax_get_json();</script>
</body>
</html>
Appreciate any help ahead of time!