好的,我是PHP的新手,所以请和我一起努力学习这些东西。无论如何,所以我有一个php页面获取JSON数据内容并在页面上回显它就好了。所以我的问题是我的JSON在多维方面被标记为:
{"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"},
那么我将如何访问第一个元素中的percentOff?这是我的javascript函数,我用它来理想地抓取元素并根据抓取“percentOff”元素做出决定。
<!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>
提前感谢任何帮助!
答案 0 :(得分:1)
{“product”:[{“brandId”:“632”,“brandName”:“Sam Edelman”,“productId”:“7515478”,“productName”:“Gigi”,“styles”:[{“价格 “:” $ 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%”, “色”: “杏仁”}]}]}
我将其复制并粘贴到此处: http://jsoneditoronline.org/
<?php
$json = '{"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"}]}]}';
$data = json_decode($json,true);
echo $data['product'][0]['styles'][0]['percentOff'];
返回'0%';
BTW“foreach”派上用场......e.g。
foreach ($data['product'][0]['styles'][0] as $key => $value) {
echo "$key: $value<br>";
}
返回:
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
foreach ($data as $key1 => $obj1) {
echo "Level 1: $key1: $obj1<br>";
foreach ($obj1 as $key2 => $obj2) {
echo "Level 2: $key2: $obj2<br>";
foreach ($obj2 as $key3 => $obj3) {
echo "Level 3: $key3: $obj3<br>";
if (is_array($obj3)) {
foreach ($obj3 as $key4 => $obj4) {
echo "Level 4: $key4: $obj4<br>";
if (is_array($obj4)) {
foreach ($obj4 as $key5 => $obj5) {
echo "Level 5: $key5: $obj5<br>";
if ($key5 == 'percentOff') {
echo 'Percent Off is: '.$obj5.' or '.$obj4['percentOff'].'<br>';
}
}
}
}
}
}
}
}
返回:
Level 1: product: Array
Level 2: 0: Array
Level 3: brandId: 632
Level 3: brandName: Sam Edelman
Level 3: productId: 7515478
Level 3: productName: Gigi
Level 3: styles: Array
Level 4: 0: Array
Level 5: price: $64.95
Level 5: styleId: 1788226
Level 5: imageUrl: http://www.zappos.com/images/z/1/7/8/8/2/2/1788226-p-DETAILED.jpg
Level 5: originalPrice: $64.95
Level 5: productUrl: http://www.zappos.com/product/7515478/color/25
Level 5: percentOff: 0%
Level 5: color: Almond