无法解决YQL中的单个输出(响应)问题

时间:2016-02-18 15:52:58

标签: javascript yql

我在使用YQL和JavaScript进行一些RSS feed解析时遇到问题。只要有多个结果,代码就可以正常工作。 YQL输出作为数组(JSON)返回,我可以使用for循环对其进行格式化。但是,当只有一个结果时,YQL返回一个对象,并且解析不起作用。有关解决方法的任何建议吗?几个小时都在尝试不同的方法,但无济于事。

<!DOCTYPE html>
<html lang="no">
    <head>
        <title>Kolonnekj&oslash;ring.no</title>

        <link href="css/bootstrap.css" rel="stylesheet">
        <script src="http://yui.yahooapis.com/3.18.1/build/yui/yui-min.js"></script>

        <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
        <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
        <!--[if lt IE 9]>
        <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
        <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
        <![endif]-->

    </head>
    <body>
        <nav class="navbar navbar-default">
            <div class="container-fluid">

                <!-- start collapsed navbar-->
                <div class="navbar-header">
                    <button 
                    type="button" 
                    class="navbar-toggle collapsed" 
                    data-toggle="collapse" 
                    data-target="#main_navbar" 
                    aria-expanded="false">
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                    </button>           
                    <a class="navbar-brand" href="#">Kolonnekj&oslash;ring.no</a>
                </div>
                <!-- / collapsed navbar-->

            <!-- start un-collapsed navbar-->
            <div 
            class="collapse navbar-collapse" 
            id="main_navbar">
                <ul class="nav navbar-nav">
                    <li class="active"><a href="index.html">Aktive kolonner</a></li>
                    <li><a href="sor_norge.html">Fjelloverganger i S&oslash;r-Norge</a></li>
                    <li><a href="nord_norge.html">Fjelloverganger i Nord-Norge</a></li>
                    <li><a href="#">Om Kolonnekj&oslash;ring.no</a></li>
                </ul>
            </div> <!-- / un-collapsed navbar-->

            </div> <!-- /container -->
        </nav>

        <div class="container">
            <h1>Aktive kolonner i Norge</h1>       
            <!-- start breadcrumb -->
            <ol class="breadcrumb">
                <li><a href="index.html">Aktive kolonner</a></li>
            </ol>
            <!-- / breadcrumb -->            
            <div class="row">
                <div class="col-md-12" id="kolonnemeldinger"></div>
            </div>
        </div>

        <script>
            YUI().use('yql', function(Y){
                var query = 'select * from rss where url = "http://www.vegvesen.no/trafikk/xml/search.rss?searchFocus.messageType=18&searchFocus.sortOrder=3"'
                var q = Y.YQL(query, function(r){
                    //r now contains the result of the YQL Query as a JSON
                    var feedmarkup = '<p>'
                    var feed = r.query.results.item // get feed as array of entries
                    for (var i=0; i<feed.length; i++){
                        feedmarkup += '<h4>' + feed[i].title + '</h4>'
                        feedmarkup += feed[i].description + '</p>'
                    }
                    document.getElementById('kolonnemeldinger').innerHTML = feedmarkup
                })
            })
        </script>

        <!-- load JS -->
        <script src="js/jquery.min.js"></script>
        <script src="js/bootstrap.min.js"></script>
        <!-- / load JS -->

    </body>
</html>

2 个答案:

答案 0 :(得分:2)

测试数组

这是Web服务数据的常见问题。解决方案是将单个项目结果转换为数组。

首次使用(typeof item ==&#39; Array&#39;)将不起作用,因为它是一个Object。

使用Array.isArray(item)将起作用,但仅适用于现代浏览器。

测试数组方法是另一种可能的解决方案,例如if(item.push)A();否则B();

有关详细信息,请参阅:Check if object is array?

YQL解决方案

然而,这些测试都不需要,因为Yahoo YQL已经提供了数据序言中的信息: result.query.count

解决方案很简单:

feed = r.query.count > 1 ? r.query.results.item : [r.query.results.item];

将单个项目转换为可由循环处理的数组。

运行下面的代码段进行测试:

&#13;
&#13;
var xhr, url, r, feedmarkup, feed, i, query;

// remove the query parameter to return multiple items
query = 'select * from rss where ' +
  'url=\'http://www.vegvesen.no/trafikk/xml/search.rss' +
  '?searchFocus.messageType=18\'';


url = 'https://query.yahooapis.com/v1/public/yql' +
  '?diagnostics=true' +
  '&format=json' +
  '&q=' + encodeURI(query) +
  '&callback=';


xhr = new XMLHttpRequest();
xhr.open('GET', url, false); // async for tests only
xhr.send();

r = JSON.parse(xhr.responseText);
document.getElementById('debug').innerHTML = JSON.stringify(r, null, '  ');

feed = r.query.count > 1 ? r.query.results.item : [r.query.results.item];
//feed = r.query.results.item.push ? r.query.results.item : [r.query.results.item];


feedmarkup = '<p>';
for (i = 0; i < feed.length; i++) {
  feedmarkup += '<h4>' + feed[i].title + '</h4>;'
  feedmarkup += feed[i].description + '</p>';
}

document.getElementById('kolonnemeldinger').innerHTML = feedmarkup;
&#13;
<h3>RSS:</h3>
<div id="kolonnemeldinger">Fetching data...</div>
<h3>JSON</h3>
<xmp id="debug"></xmp>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

YUI().use('yql', function(Y){
                var query = 'select * from rss where url = "http://www.vegvesen.no/trafikk/xml/search.rss?searchFocus.messageType=18&searchFocus.sortOrder=3"'
                var q = Y.YQL(query, function(r){
                    //r now contains the result of the YQL Query as a JSON
                    var feedmarkup = '<p>'
                    var feed = r.query.results.item // get feed as array of entries
                    feed = Array.isArray(feed) ? feed : [feed];
                    for (var i=0; i<feed.length; i++){
                        feedmarkup += '<h4>' + feed[i].title + '</h4>'
                        feedmarkup += feed[i].description + '</p>'
                    }
                    document.getElementById('kolonnemeldinger').innerHTML = feedmarkup
                })
            })