使用PHP循环使用JSON(来自谷歌电子表格)

时间:2014-04-04 17:09:42

标签: php json google-apps-script

我得到了为每个人使用'的概念。循环并输出存储的json对象的结果。

但是,我完全坚持将逻辑应用于来自谷歌电子表格的JSON。

理想情况下,我想做的是循环播放,只输出gsx $ text和gsx $ url的值。我认为我无法正确定位数组值。

感谢您的帮助!!

至于我获得的代码:

<?php

$url = "http://spreadsheets.google.com/feeds/list/0AucrbcW2_KdddFR1ZmhaYmUwc0FtNm1uTV9pMGZTUUE/od6/public/values?alt=json&amp;callback=displayContent";

$json = file_get_contents($url);
$data = json_decode($json, TRUE);

//print_r ($data);

foreach($data as $item) {
    echo $item['feed']['entry']['gsx$text']['$t'];
    echo $item['feed']['entry']['gsx$url']['$t'];
}

?>

以下是我的Google电子表格中的示例JSON:

{
    "version": "1.0",
    "encoding": "UTF-8",
    "feed": {
        "xmlns": "http://www.w3.org/2005/Atom",
        "xmlns$openSearch": "http://a9.com/-/spec/opensearchrss/1.0/",
        "xmlns$gsx": "http://schemas.google.com/spreadsheets/2006/extended",
        "id": {
            "$t": "https://spreadsheets.google.com/feeds/list/0AucrbcW2_KdddFR1ZmhaYmUwc0FtNm1uTV9pMGZTUUE/od6/public/values"
        },
        "updated": {
            "$t": "2014-04-04T16:14:33.594Z"
        },
        "category": [
            {
                "scheme": "http://schemas.google.com/spreadsheets/2006",
                "term": "http://schemas.google.com/spreadsheets/2006#list"
            }
        ],
        "title": {
            "type": "text",
            "$t": "Sheet1"
        },
        "link": [
            {
                "rel": "alternate",
                "type": "text/html",
                "href": "https://spreadsheets.google.com/pub?key=0AucrbcW2_KdddFR1ZmhaYmUwc0FtNm1uTV9pMGZTUUE"
            },
            {
                "rel": "http://schemas.google.com/g/2005#feed",
                "type": "application/atom+xml",
                "href": "https://spreadsheets.google.com/feeds/list/0AucrbcW2_KdddFR1ZmhaYmUwc0FtNm1uTV9pMGZTUUE/od6/public/values"
            },
            {
                "rel": "self",
                "type": "application/atom+xml",
                "href": "https://spreadsheets.google.com/feeds/list/0AucrbcW2_KdddFR1ZmhaYmUwc0FtNm1uTV9pMGZTUUE/od6/public/values?alt=json"
            }
        ],
        "author": [
            {
                "name": {
                    "$t": "michael.daul"
                },
                "email": {
                    "$t": "michael.daul@gmail.com"
                }
            }
        ],
        "openSearch$totalResults": {
            "$t": "2"
        },
        "openSearch$startIndex": {
            "$t": "1"
        },
        "entry": [
            {
                "id": {
                    "$t": "https://spreadsheets.google.com/feeds/list/0AucrbcW2_KdddFR1ZmhaYmUwc0FtNm1uTV9pMGZTUUE/od6/public/values/cre1l"
                },
                "updated": {
                    "$t": "2014-04-04T16:14:33.594Z"
                },
                "category": [
                    {
                        "scheme": "http://schemas.google.com/spreadsheets/2006",
                        "term": "http://schemas.google.com/spreadsheets/2006#list"
                    }
                ],
                "title": {
                    "type": "text",
                    "$t": "1"
                },
                "content": {
                    "type": "text",
                    "$t": "end: 5, text: this is a test entry, url: http://www.google.com"
                },
                "link": [
                    {
                        "rel": "self",
                        "type": "application/atom+xml",
                        "href": "https://spreadsheets.google.com/feeds/list/0AucrbcW2_KdddFR1ZmhaYmUwc0FtNm1uTV9pMGZTUUE/od6/public/values/cre1l"
                    }
                ],
                "gsx$start": {
                    "$t": "1"
                },
                "gsx$end": {
                    "$t": "5"
                },
                "gsx$text": {
                    "$t": "this is a test entry"
                },
                "gsx$url": {
                    "$t": "http://www.google.com"
                }
            },
            {
                "id": {
                    "$t": "https://spreadsheets.google.com/feeds/list/0AucrbcW2_KdddFR1ZmhaYmUwc0FtNm1uTV9pMGZTUUE/od6/public/values/chk2m"
                },
                "updated": {
                    "$t": "2014-04-04T16:14:33.594Z"
                },
                "category": [
                    {
                        "scheme": "http://schemas.google.com/spreadsheets/2006",
                        "term": "http://schemas.google.com/spreadsheets/2006#list"
                    }
                ],
                "title": {
                    "type": "text",
                    "$t": "6"
                },
                "content": {
                    "type": "text",
                    "$t": "end: 10, text: this is also a test, url: http://www.yahoo.com"
                },
                "link": [
                    {
                        "rel": "self",
                        "type": "application/atom+xml",
                        "href": "https://spreadsheets.google.com/feeds/list/0AucrbcW2_KdddFR1ZmhaYmUwc0FtNm1uTV9pMGZTUUE/od6/public/values/chk2m"
                    }
                ],
                "gsx$start": {
                    "$t": "6"
                },
                "gsx$end": {
                    "$t": "10"
                },
                "gsx$text": {
                    "$t": "this is also a test"
                },
                "gsx$url": {
                    "$t": "http://www.yahoo.com"
                }
            }
        ]
    }
}

3 个答案:

答案 0 :(得分:1)

看起来你要迭代的数组是

$data['feed']['entry']

所以

foreach ($data['feed']['entry'] as $item) {
  echo $item['gsx$text']['$t'];
  echo $item['gsx$url']['$t'];
}

答案 1 :(得分:1)

如果以这种方式访问​​它,则不必循环遍历它。

echo $data['feed']['entry']['gsx$text']['$t'];
echo $data['feed']['entry']['gsx$url']['$t'];

答案 2 :(得分:0)

GitHub上的库。依赖性很好:)