拼接 YouTube feed JSON 结果
假设我使用 Google Feed API 从 YouTube 获得了以下(虚假)Feed 结果。
{ "feed" : { "author" : "YouTube",
"description" : "",
"entries" : [ { "author" : "Foo",
"categories" : [ "http://gdata.youtube.com/schemas/2007#video",
"Entertainment",
"golf",
"resort"
],
"content" : "Four friends are playing golf. Originally aired on 3/22/2011.",
"contentSnippet" : "Four friends are still playing golf. Originally aired on ...",
"link" : "http://www.linktoyoutubevid.com",
"publishedDate" : "Thu, 24 Mar 2011 07:07:13 -0700",
"title" : "Foobar"
},
.....more feed entries here.....
],
"feedUrl" : "http://www.myyoutubefeed.com",
"link" : "http://www.youtube.com",
"title" : "YouTube Videos",
"type" : "atom10"
},
"m" : "json",
"status" : { "code" : 200 }
}
我想将此提要结果(保留 JSON 格式)保存到变量中,但我首先想删除类别、内容和 contentSnippet 等元素。我尝试使用 .splice 但不断收到此错误: 错误:feedResult.feed.entries[0].splice 不是函数
这是我的 feed 调用的一部分:
feedUrl: opts.sourceUrl,
type: "video",
numEntries: 3,
onFeedLoad: function(feedResult) {
//alert(feedResult);
formatVids(feedResult);
var test = feedResult.feed.entries[0].splice(1,3);
//alert(test);
What's the right way to use .splice in这个场景?我可能会很困惑是否需要拼接 JSON 字符串或 JSON 对象。有更好的解决方案吗?
谢谢!
Say I have the following (phoney) feed result from YouTube using the Google Feed API.
{ "feed" : { "author" : "YouTube",
"description" : "",
"entries" : [ { "author" : "Foo",
"categories" : [ "http://gdata.youtube.com/schemas/2007#video",
"Entertainment",
"golf",
"resort"
],
"content" : "Four friends are playing golf. Originally aired on 3/22/2011.",
"contentSnippet" : "Four friends are still playing golf. Originally aired on ...",
"link" : "http://www.linktoyoutubevid.com",
"publishedDate" : "Thu, 24 Mar 2011 07:07:13 -0700",
"title" : "Foobar"
},
.....more feed entries here.....
],
"feedUrl" : "http://www.myyoutubefeed.com",
"link" : "http://www.youtube.com",
"title" : "YouTube Videos",
"type" : "atom10"
},
"m" : "json",
"status" : { "code" : 200 }
}
I want to save this feed result (keeping the JSON format) to a variable, but I first want to strip out elements like categories, content, and contentSnippet. I tried using .splice but kept getting this error: Error: feedResult.feed.entries[0].splice is not a function
Here's part of my feed call:
feedUrl: opts.sourceUrl,
type: "video",
numEntries: 3,
onFeedLoad: function(feedResult) {
//alert(feedResult);
formatVids(feedResult);
var test = feedResult.feed.entries[0].splice(1,3);
//alert(test);
What's the correct way to use .splice in this scenario? I could be getting confused as to whether I need to be splicing a JSON string or the JSON object. Is there a better solution?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不幸的是,在一个非常重要的方面,对象键并不等同于数组键:处理对象键时不会保留顺序。
完成您想要做的事情的一种方法是:
关于 IE 兼容性问题,以下内容来自 MDC 文档。
兼容性
图是 ECMA-262 标准的最新补充内容;因此,它可能不会出现在该标准的其他实现中。您可以通过在脚本开头插入以下代码来解决此问题,从而允许在本身不支持映射的实现中使用映射。该算法与 ECMA-262 第 5 版中指定的算法完全相同,假设 Object、TypeError 和 Array 具有其原始值,并且 fun.call 的计算结果为 Function.prototype.call 的原始值。
Unfortunately Object keys are not equivalent to Array keys, in one very important aspect: Order is not preserved when dealing with Object keys.
One way to accomplish what you are trying to do is:
With regard to the IE compatability issue the following is from the MDC documentation on the subject.
Compatibility
map is a recent addition to the ECMA-262 standard; as such it may not be present in other implementations of the standard. You can work around this by inserting the following code at the beginning of your scripts, allowing use of map in implementations which do not natively support it. This algorithm is exactly the one specified in ECMA-262, 5th edition, assuming Object, TypeError, and Array have their original values and that fun.call evaluates to the original value of Function.prototype.call.