拼接 YouTube feed JSON 结果

发布于 2024-11-05 02:25:17 字数 1487 浏览 0 评论 0原文

假设我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

落日海湾 2024-11-12 02:25:17

不幸的是,在一个非常重要的方面,对象键并不等同于数组键:处理对象键时不会保留顺序。

完成您想要做的事情的一种方法是:

var resultFeed = feedResult.feed.entries.map( function(entry){
  delete entry.category;
  delete entry.content;
  delete entry.contentSnippet;
  return entry;
})

关于 IE 兼容性问题,以下内容来自 MDC 文档

兼容性

图是 ECMA-262 标准的最新补充内容;因此,它可能不会出现在该标准的其他实现中。您可以通过在脚本开头插入以下代码来解决此问题,从而允许在本身不支持映射的实现中使用映射。该算法与 ECMA-262 第 5 版中指定的算法完全相同,假设 Object、TypeError 和 Array 具有其原始值,并且 fun.call 的计算结果为 Function.prototype.call 的原始值。

if (!Array.prototype.map)
{
  Array.prototype.map = function(fun /*, thisp */)
  {
    "use strict";

    if (this === void 0 || this === null)
      throw new TypeError();

    var t = Object(this);
    var len = t.length >>> 0;
    if (typeof fun !== "function")
      throw new TypeError();

    var res = new Array(len);
    var thisp = arguments[1];
    for (var i = 0; i < len; i++)
    {
      if (i in t)
        res[i] = fun.call(thisp, t[i], i, t);
    }

    return res;
  };
}

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:

var resultFeed = feedResult.feed.entries.map( function(entry){
  delete entry.category;
  delete entry.content;
  delete entry.contentSnippet;
  return entry;
})

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.

if (!Array.prototype.map)
{
  Array.prototype.map = function(fun /*, thisp */)
  {
    "use strict";

    if (this === void 0 || this === null)
      throw new TypeError();

    var t = Object(this);
    var len = t.length >>> 0;
    if (typeof fun !== "function")
      throw new TypeError();

    var res = new Array(len);
    var thisp = arguments[1];
    for (var i = 0; i < len; i++)
    {
      if (i in t)
        res[i] = fun.call(thisp, t[i], i, t);
    }

    return res;
  };
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文