【发布时间】:2016-03-29 17:48:25
【问题描述】:
我得到了使用“for each”循环并从存储的 json 对象中输出结果的概念。
但是,我完全坚持将逻辑应用于来自谷歌电子表格的 JSON。
理想情况下,我想做的是循环并仅输出 gsx$text 和 gsx$url 的值。我想我只是无法正确定位数组值。
感谢您的帮助!!
这是我得到的代码:
<?php
$url = "http://spreadsheets.google.com/feeds/list/0AucrbcW2_KdddFR1ZmhaYmUwc0FtNm1uTV9pMGZTUUE/od6/public/values?alt=json&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'];
}
?>
这是我的谷歌电子表格中的示例 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"
}
}
]
}
}
【问题讨论】:
-
它看起来不像
$data是一个数组,所以foreach不起作用。检查您已注释掉的print_r语句的结果 - 这将告诉您如何引用$data中的元素。 -
感谢 Kryten - 我认为问题在于使用来自 google 的 'json-in-script' 调用,而不仅仅是 'json';我已经编辑了上面的代码。
标签: php json google-apps-script