donglan8999 2014-02-17 01:55
浏览 48
已采纳

通过php访问多维JSON数据。

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!

  • 写回答

1条回答 默认 最新

  • dongtang5057 2014-02-17 02:09
    关注

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

    I copied and pasted it into here: 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%';

    BTW "foreach" comes in handy....

    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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,
  • ¥15 spaceclaim模型变灰色
  • ¥15 求一份华为esight平台V300R009C00SPC200这个型号的api接口文档
  • ¥15 字符串比较代码的漏洞
  • ¥15 欧拉系统opt目录空间使用100%
  • ¥15 ul做导航栏格式不对怎么改?