Javascript 使用Jquery处理JSON数据
以下是我的JSON数据: 它基本上是一个JSON格式的谷歌电子表格 我想得到的是以美元开始的钱。在谷歌表单中,它只在C列。尤其是现在,我试图得到12美元的单元格:C4 我尝试了$.results.prepend+data.feed.entry.gsx$\u cpzh4.$t+;得到: 未捕获的TypeError:无法读取未定义的属性“$t” 注意:请保持您的答案简单,因为我12小时前才开始学习javascript和jquery。 首先,条目是一个数组 并且您尝试访问的gsx$_cpzh4属性并不存在于条目数组中的所有对象中 您需要使用simple forEach进行迭代,并使用.hasOwnProperty访问所有具有该属性的对象,下面给出了一个简单的控制台打印演示,使用您提供的json,您可以看到它使用entry.gsx$_cpzh4.$t为entry数组中的所有条目打印价格,并跳过未定义该属性的条目Javascript 使用Jquery处理JSON数据,javascript,jquery,json,Javascript,Jquery,Json,以下是我的JSON数据: 它基本上是一个JSON格式的谷歌电子表格 我想得到的是以美元开始的钱。在谷歌表单中,它只在C列。尤其是现在,我试图得到12美元的单元格:C4 我尝试了$.results.prepend+data.feed.entry.gsx$\u cpzh4.$t+;得到: 未捕获的TypeError:无法读取未定义的属性“$t” 注意:请保持您的答案简单,因为我12小时前才开始学习javascript和jquery。 首先,条目是一个数组 并且您尝试访问的gsx$_cpzh4属性并不
//iterate over al lthe entries
data.feed.entry.forEach(function(entry) {
//if it has the property defined with name gsx$_cpzh4
if (entry.hasOwnProperty('gsx$_cpzh4')) {
//print the prices
console.log(entry['gsx$_cpzh4'].$t);
}
});
让数据={
版本:1.0,
编码:UTF-8,
提要:{
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,
身份证:{
$t:https://spreadsheets.google.com/feeds/list/12fmjZfRgJHlSulaWQdmvaz1yinmwaTwsdHsOzRfYNBE/od6/public/values
},
更新:{
$t:2018-06-23T22:12:49.197Z
},
类别:[{
方案:http://schemas.google.com/spreadsheets/2006,
术语:http://schemas.google.com/spreadsheets/2006list
}],
标题:{
类型:文本,
$t:1张
},
链接:[{
雷尔:候补,
类型:application/atom+xml,
href:https://docs.google.com/spreadsheets/d/12fmjZfRgJHlSulaWQdmvaz1yinmwaTwsdHsOzRfYNBE/pubhtml
},
{
rel:http://schemas.google.com/g/2005feed,
类型:application/atom+xml,
href:https://spreadsheets.google.com/feeds/list/12fmjZfRgJHlSulaWQdmvaz1yinmwaTwsdHsOzRfYNBE/od6/public/values
},
{
rel:http://schemas.google.com/g/2005post,
类型:application/atom+xml,
href:https://spreadsheets.google.com/feeds/list/12fmjZfRgJHlSulaWQdmvaz1yinmwaTwsdHsOzRfYNBE/od6/public/values
},
{
雷尔:赛尔夫,
类型:application/atom+xml,
href:https://spreadsheets.google.com/feeds/list/12fmjZfRgJHlSulaWQdmvaz1yinmwaTwsdHsOzRfYNBE/od6/public/values?alt=json
}
],
作者:[{
姓名:{
$t:7008
},
电邮:{
$t:gem7008@gmail.com
}
}],
openSearch$totalResults:{
$t:4
},
openSearch$startIndex:{
$t:1
},
条目:[{
身份证:{
$t:https://spreadsheets.google.com/feeds/list/12fmjZfRgJHlSulaWQdmvaz1yinmwaTwsdHsOzRfYNBE/od6/public/values/cokwr
},
更新:{
$t:2018-06-23T22:12:49.197Z
},
类别:[{
方案:http://schemas.google.com/spreadsheets/2006,
术语:http://schemas.google.com/spreadsheets/2006list
}],
标题:{
类型:文本,
$t:Row:2
},
内容:{
类型:文本,
$t:_cokwr:品目1
},
链接:[{
雷尔:赛尔夫,
类型:application/atom+xml,
href:https://spreadsheets.google.com/feeds/list/12fmjZfRgJHlSulaWQdmvaz1yinmwaTwsdHsOzRfYNBE/od6/public/values/cokwr
}],
gsx$_cokwr:{
$t:标题1
}
},
{
身份证:{
$t:https://spreadsheets.google.com/feeds/list/12fmjZfRgJHlSulaWQdmvaz1yinmwaTwsdHsOzRfYNBE/od6/public/values/cpzh4
},
更新:{
$t:2018-06-23T22:12:49.197Z
},
类别:[{
方案:http://schemas.google.com/spreadsheets/2006,
术语:http://schemas.google.com/spreadsheets/2006list
}],
标题:{
类型:文本,
$t:Row:3
},
内容:{
类型:文本,
$t:$cokwr:John,$cpzh4:$9.00
},
链接:[{
雷尔:赛尔夫,
类型:application/atom+xml,
href:https://spreadsheets.google.com/feeds/list/12fmjZfRgJHlSulaWQdmvaz1yinmwaTwsdHsOzRfYNBE/od6/public/values/cpzh4
}],
gsx$_cokwr:{
$t:约翰
},
gsx$\u cpzh4:{
$t:$9.00
}
},
{
身份证:{
$t:https://spreadsheets.google.com/feeds/list/12fmjZfRgJHlSulaWQdmvaz1yinmwaTwsdHsOzRfYNBE/od6/public/values/cre1l
},
更新:{
$t:2018-06-23T22:12:49.197Z
},
类别:[{
方案:http://schemas.google.com/spreadsheets/2006,
术语:http://schemas.google.com/spreadsheets/2006list
}],
标题:{
类型:文本,
$t:Row:4
},
内容:{
类型:文本,
$t:$cokwr:Elijah,$cpzh4:$12.00
},
链接:[{
雷尔:赛尔夫,
类型:application/atom+xml,
href:https://spreadsheets.google.com/feeds/list/12fmjZfRgJHlSulaWQdmvaz1yinmwaTwsdHsOzRfYNBE/od6/public/values/cre1l
}],
gsx$_cokwr:{
$t:以利亚
},
gsx$\u cpzh4:{
$t:$12.00
}
},
{
身份证:{
$t:https://spreadsheets.google.com/feeds/list/12fmjZfRgJHlSulaWQdmvaz1yinmwaTwsdHsOzRfYNBE/od6/public/values/chk2m
},
更新:{
$t:2018-06-23T22:12:49.197Z
},
类别:[{
方案:http://schemas.google.com/spreadsheets/2006,
术语:http://schemas.google.com/spreadsheets/2006list
}],
标题:{
类型:文本,
$t:Row:5
},
内容:{
类型:文本,
$t:$cokwr:Andrew,$cpzh4:$1.00
},
链接:[{
雷尔:赛尔夫,
类型:application/atom+xml,
href:https://spreadsheets.google.com/feeds/list/12fmjZfRgJHlSulaWQdmvaz1yinmwaTwsdHsOzRfYNBE/od6/public/values/chk2m
}],
gsx$_cokwr:{
$t:Andrew
},
gsx$\u cpzh4:{
$t:$1.00
}
}
]
}
}
data.feed.entry.forEachfunctionentry{
如果条目.hasOwnProperty'gsx$\u cpzh4'{
console.logentry['gsx$\U cpzh4'].$t;
$.results.prepend+条目['gsx$_cpzh4'].$t+
}
};
寻求调试帮助的问题此代码为什么不起作用?必须包括期望的行为,特定问题或错误,以及在问题本身中重现该问题所需的最短工作代码段。@LGSon所需行为:尝试获取$12.00单元格:C4特定问题或错误:未捕获类型错误:无法读取未定义的最短工作代码段的属性“$t”:$.results.prepend+data.feed.entry.gsx$\u cpzh4.$t+;如果你想说JSON数据很长,那么:1我是一个初学者,不知道如何从中创建一个片段来重现我的问题2它只是一块数据,可以很容易地复制粘贴以重现问题。一般来说,一个问题的海报是否是初学者并不重要,对于任何能够提供正确答案的人来说,都需要一段代码片段来重现这个问题。此外,一个人应该提供一个答案,因为这个链接解释了更多关于这个问题的内容,同样,不管你的技能如何。不过,在这种情况下,你很幸运,有人决定写这样的片段,但不要依赖它,因为通常没有人会这样做,这样的问题通常会被否决/closed@LGSon我懂了。我会记住的。
//iterate over al lthe entries
data.feed.entry.forEach(function(entry) {
//if it has the property defined with name gsx$_cpzh4
if (entry.hasOwnProperty('gsx$_cpzh4')) {
//print the prices
console.log(entry['gsx$_cpzh4'].$t);
}
});