通过php访问多维JSON数据。

时间:2021-10-02 15:55:26

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:

好吧,我是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"},

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.

那么我将如何访问第一个元素中的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>

Appreciate any help ahead of time!

提前感谢任何帮助!

1 个解决方案

#1


1  

{"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"}]}]}

{“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%”, “色”: “杏仁” }]}]}

I copied and pasted it into here: http://jsoneditoronline.org/

我复制并粘贴到这里: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'];

returns '0%';

返回'0%';

BTW "foreach" comes in handy....

BTW“foreach”派上用场......

e.g.

例如

foreach ($data['product'][0]['styles'][0] as $key => $value) {
    echo "$key: $value<br>";
}

returns:

收益:

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>';
                            }
                        }
                    }
                }
            }
        }
    }
}

returns:

收益:

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

#1


1  

{"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"}]}]}

{“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%”, “色”: “杏仁” }]}]}

I copied and pasted it into here: http://jsoneditoronline.org/

我复制并粘贴到这里: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'];

returns '0%';

返回'0%';

BTW "foreach" comes in handy....

BTW“foreach”派上用场......

e.g.

例如

foreach ($data['product'][0]['styles'][0] as $key => $value) {
    echo "$key: $value<br>";
}

returns:

收益:

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>';
                            }
                        }
                    }
                }
            }
        }
    }
}

returns:

收益:

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