【问题标题】:How to parse "og:description" node in json object from Google Custom Search API如何从 Google 自定义搜索 API 解析 json 对象中的“og:description”节点
【发布时间】:2016-03-07 17:54:22
【问题描述】:

我正在使用 Google 自定义搜索 API,它为我提供了 json 格式的搜索结果。 (Docs)

我需要获取节点og:description 的内容,但显然它并不像我想象的那么简单。我试过使用这个:items.pagemap.metatags[0].og:description 这不起作用 - 分号会导致错误。

我的其余代码看起来像这样并且正在运行:

    var key = "xxx";
    var cx = "xxx";
    var q = "cars";
    var url = "https://www.googleapis.com/customsearch/v1?q=" + encodeURIComponent(q) + " &prettyPrint=false&cx=" + cx + "&key=" + key + "";
    $.getJSON(url, function(data) {
        var news = [];
        if (data.items) {
            $.each(data.items, function(key, i) {
                news.push("<li><img src='" + i.pagemap.cse_image[0].src + "'><a target='_blank' href='" + i.link + "'>" + i.title + "</a><div class='description'>" + i.snippet + "</div></li>");
                //console.log(data);
            });
        }

        $("<ul/>", {
            html: news.join("")
        }).appendTo("#content_0");
    }).fail(function() {
        console.log("error");
    });

非常感谢任何帮助。

【问题讨论】:

    标签: jquery json google-api google-search-api


    【解决方案1】:

    经过几个小时的搜索,结果发现解决方案是这样的——方括号:

    items.pagemap.metatags[0]['og:description']
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多