【问题标题】:Getting JSON data from Google Spreadsheets - Uncaught TypeError从 Google 电子表格获取 JSON 数据 - 未捕获的 TypeError
【发布时间】:2014-08-09 04:59:46
【问题描述】:

我尝试了几种方法,包括这篇文章中的一种:JSON data from google spreadsheet

但是,这些都不起作用。我在谷歌文档中创建了一个简单的测试表,并发布了它。您可以查看电子表格的发布版本 here 供您参考。

Here is the direct link to the JSON data,对于那些无法访问链接的人,请参阅下面发布的 JSON 数据。

这是我当前的 jQuery JSON 代码:(注意:我使用的 url 是有效的,但由于它的长度,我没有在下面包含它)

$.getJSON('<json-data-url>', function(data) {
    var string = "<table>";       
    for (var i = 0; i < data.feed.entry.length; i++) {

      var itemNum = data.feed.entry[i].gsx$itemnum.$t;
      var cost = data.feed.entry[i].gsx$cost.$t;
      var msrp = data.feed.entry[i].gsx$msrp.$t;
      string += '<tr><td>' + itemNum + '</td><td>' + cost + '</td><td>' + msrp + '</td></tr>';

    }

    string += "</table>";

    $(string).appendTo('body');
});

返回的当前错误是:Uncaught TypeError: Cannot read property '$t' of underfined

Here is a Demo Fiddle 供您测试

我认为这个错误主要是因为我无法准确读取原始 JSON 文件,所以我的选择器是错误的。

原始 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/17UFRY5IHNNA08M3C6J7KLl_mtZxYNtiZNJ3aPXhfEaM/od6/public/basic"},"updated":{"$t":"2014-06-18T17:26:03.848Z"},"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":"application/atom+xml","href":"https://docs.google.com/spreadsheets/d/17UFRY5IHNNA08M3C6J7KLl_mtZxYNtiZNJ3aPXhfEaM/pubhtml?hl\u003den_US"},{"rel":"http://schemas.google.com/g/2005#feed","type":"application/atom+xml","href":"https://spreadsheets.google.com/feeds/list/17UFRY5IHNNA08M3C6J7KLl_mtZxYNtiZNJ3aPXhfEaM/od6/public/basic"},{"rel":"http://schemas.google.com/g/2005#post","type":"application/atom+xml","href":"https://spreadsheets.google.com/feeds/list/17UFRY5IHNNA08M3C6J7KLl_mtZxYNtiZNJ3aPXhfEaM/od6/public/basic"},{"rel":"self","type":"application/atom+xml","href":"https://spreadsheets.google.com/feeds/list/17UFRY5IHNNA08M3C6J7KLl_mtZxYNtiZNJ3aPXhfEaM/od6/public/basic?alt\u003djson"}],"author":[{"name":{"$t":"MetSales1"},"email":{"$t":"metsales1@gmail.com"}}],"openSearch$totalResults":{"$t":"4"},"openSearch$startIndex":{"$t":"1"},"entry":[{"id":{"$t":"https://spreadsheets.google.com/feeds/list/17UFRY5IHNNA08M3C6J7KLl_mtZxYNtiZNJ3aPXhfEaM/od6/public/basic/cokwr"},"updated":{"$t":"2014-06-18T17:26:03.848Z"},"category":[{"scheme":"http://schemas.google.com/spreadsheets/2006","term":"http://schemas.google.com/spreadsheets/2006#list"}],"title":{"type":"text","$t":"Linea Pro"},"content":{"type":"text","$t":"cost: $590, msrp: $690"},"link":[{"rel":"self","type":"application/atom+xml","href":"https://spreadsheets.google.com/feeds/list/17UFRY5IHNNA08M3C6J7KLl_mtZxYNtiZNJ3aPXhfEaM/od6/public/basic/cokwr"}]},{"id":{"$t":"https://spreadsheets.google.com/feeds/list/17UFRY5IHNNA08M3C6J7KLl_mtZxYNtiZNJ3aPXhfEaM/od6/public/basic/cpzh4"},"updated":{"$t":"2014-06-18T17:26:03.848Z"},"category":[{"scheme":"http://schemas.google.com/spreadsheets/2006","term":"http://schemas.google.com/spreadsheets/2006#list"}],"title":{"type":"text","$t":"Infinea Tab"},"content":{"type":"text","$t":"cost: $690, msrp: $790"},"link":[{"rel":"self","type":"application/atom+xml","href":"https://spreadsheets.google.com/feeds/list/17UFRY5IHNNA08M3C6J7KLl_mtZxYNtiZNJ3aPXhfEaM/od6/public/basic/cpzh4"}]},{"id":{"$t":"https://spreadsheets.google.com/feeds/list/17UFRY5IHNNA08M3C6J7KLl_mtZxYNtiZNJ3aPXhfEaM/od6/public/basic/cre1l"},"updated":{"$t":"2014-06-18T17:26:03.848Z"},"category":[{"scheme":"http://schemas.google.com/spreadsheets/2006","term":"http://schemas.google.com/spreadsheets/2006#list"}],"title":{"type":"text","$t":"Touch Screen"},"content":{"type":"text","$t":"cost: $108, msrp: $208"},"link":[{"rel":"self","type":"application/atom+xml","href":"https://spreadsheets.google.com/feeds/list/17UFRY5IHNNA08M3C6J7KLl_mtZxYNtiZNJ3aPXhfEaM/od6/public/basic/cre1l"}]},{"id":{"$t":"https://spreadsheets.google.com/feeds/list/17UFRY5IHNNA08M3C6J7KLl_mtZxYNtiZNJ3aPXhfEaM/od6/public/basic/chk2m"},"updated":{"$t":"2014-06-18T17:26:03.848Z"},"category":[{"scheme":"http://schemas.google.com/spreadsheets/2006","term":"http://schemas.google.com/spreadsheets/2006#list"}],"title":{"type":"text","$t":"AIO Terminal"},"content":{"type":"text","$t":"cost: $790, msrp: $890"},"link":[{"rel":"self","type":"application/atom+xml","href":"https://spreadsheets.google.com/feeds/list/17UFRY5IHNNA08M3C6J7KLl_mtZxYNtiZNJ3aPXhfEaM/od6/public/basic/chk2m"}]}]}}

一如既往...非常感谢任何和所有帮助!

【问题讨论】:

  • 我正在查看 JSON 提要,但没有 data.feed.entry[i].gsx$itemnumgsx$itemnum 是什么?这在 JSON 中不存在,这就是您收到未定义错误的原因。
  • 同样适用于gsx$costgsx$msrp
  • @tomaroo 我第一次尝试使用 JSON。我也试过 content$itemnum.$t 没用。
  • @tomaroo 你能帮我看看选择器吗?

标签: jquery json


【解决方案1】:

使用此站点可帮助您绘制 JSON 数据并查看您需要处理的内容:

http://json.parser.online.fr/

据我所知,这可能是您的目标:

  var id = data.feed.entry[i].id.$t;
  var title = data.feed.entry[i].title.$t;
  var content = data.feed.entry[i].content.$t;

JSON 数据中似乎没有 cost 或 msrp 的属性。

编辑:我看到内容属性将成本和 msrp 信息存储在一个字符串中。除非您拆分/解析字符串,否则您无法真正使用它:

  var x = content.split(', '); // ["cost: $590", "msrp: $690"]
  var cost = x[0]; // "cost: $590"
  var msrp = x[1]; // "msrp: $690"

  var $cost = cost.split(' ')[1]; // "$590"
  var $msrp = msrp.split(' ')[1]; // "$690"

【讨论】:

  • 啊,非常酷的工具。有没有办法从内容中深入了解$t?像$t.cost 或者$t 的内容只是一个字符串?所以我必须自己解析字符串?
  • 太棒了,这很有帮助。我猜谷歌文档不使用 JSON 来填充他们的表格,因为它没有单元格(1,1)。它在 JSON 中有列 costmsrp,但没有列 Item Num
【解决方案2】:
function displayContent(json) {
var string = "<table>";      
var len = json.feed.entry.length;
for (var i=0; i<len; i++) 
{
    var itemNum = json.feed.entry[i].gsx$itemnum.$t;
    var cost = json.feed.entry[i].gsx$cost.$t;
    var msrp = json.feed.entry[i].gsx$msrp.$t;
string += '<tr><td>'+itemNum+'</td><td>'+cost+'</td><td>'+msrp+'</td></tr>';
}
string += "</table>";
$(string).appendTo('body');
}   

在此之后放置回调脚本 src="http://spreadsheets.google.com/feeds/list/17UFRY5IHNNA08M3C6J7KLl_mtZxYNtiZNJ3aPXhfEaM/od6/public/values?alt=json-in-script&callback=displayContent" type="text/javascript"

这是工作小提琴:http://jsfiddle.net/adhikari18/J98LD/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-10
    相关资源
    最近更新 更多