使用YQL进行JSON解析有什么问题?

问题描述

| 我想使用JQuery,YQL解析远程JSON文件(您知道跨域问题,因此yql最好) 但我不知道此代码中缺少什么? index.html     
<html lang=\"en\">
<head>
    <Meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"> 
    <title>untitled</title>
    <style type=\"text/css\">
        body { text-align: center; }
    </style>
</head>
<body onLoad=\"gova();\">

    <div id=\"container\">

    </div>
    <table id=\"userdata\" border=\"1\">
        <thead>
            <th>First Name</th>
            <th>Last Name</th>
            <th>Email Address</th>
            <th>City</th>
        </thead>
        <tbody></tbody>
    </table>

<script src=\"http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js\" type=\"text/javascript\" charset=\"utf-8\"></script>    
<script type=\"text/javascript\" src=\"cross-domain-requests.js\"></script>

<script type=\"text/javascript\">

function  gova() {
    var path = $(\'http://mapleleafrealities.com/jsondata.PHP\').val();

    requestCrossDomain(\'http://mapleleafrealities.com/jsondata.PHP\',function(results) {
        $(\'#container\').html(results);

    });

    return false;
}

</script>
</body>
</html>
跨域requests.js
// Accepts a url and a callback function to run.
function requestCrossDomain( site,callback ) {

    // If no url was passed,exit.
    if ( !site ) {
        alert(\'No site was passed.\');
        return false;
    }

    // Take the provided url,and add it to a YQL query. Make sure you encode it!
    var yql = \'http://query.yahooapis.com/v1/public/yql?q=\' + encodeURIComponent(\'select * from html where url=\"\' + site + \'\"\') + \'&format=xml&callback=?\';

    // Request that Ysql string,and run a callback function.
    // Pass a defined function to prevent cache-busting.

    $.getJSON( yql,cbFunc );

    function cbFunc(data) {
    // If we have something to work with...
    if ( data.results[0] ) {
        // Strip out all script tags,for security reasons.
        // BE VERY CAREFUL. This helps,but we should do more. 
        data = data.results[0].replace(/<script[^>]*>[\\s\\S]*?<\\/script>/gi,\'\');

        // If the user passed a callback,and it
        // is a function,call it,and send through the data var.
        if ( typeof callback === \'function\') {
            callback(data);
        }
    }
    // Else,Maybe we requested a site that doesn\'t exist,and nothing returned.
    else throw new Error(\'nothing returned from getJSON.\');
    }
}
我想在表中显示未格式化的数据吗?怎么样 ? 请给解决方案问题出在哪里或缺失了什么? 提前致谢 !! :)     

解决方法

           我想使用JQuery,YQL解析远程JSON文件 您说您想解析一些JSON,但是您的YQL查询请求HTML,而YQL URL请求XML响应!
var yql = \'http://query.yahooapis.com/v1/public/yql?q=\' + encodeURIComponent(\'select * from html where url=\"\' + site + \'\"\') + \'&format=xml&callback=?\';
如果您真的想使用JSON,请将该行更改为如下所示。它a)使用
json
表(因为这是
site
内容的本质),并且b)告诉YQL返回JSON,因为您正在使用
jQuery.getJSON()
函数!
var yql = \'http://query.yahooapis.com/v1/public/yql?\'
        + \'q=\' + encodeURIComponent(\'select * from json where url=@url\')
        + \'&url=\' + encodeURIComponent(site)
        + \'&format=json&callback=?\';
现在,YQL返回JSON,您可以通过
data.query.results.json
获得
json
对象,该对象随后包含
userdata
对象的数组。根据您的代码,查看更完整的示例,该示例从YQL获取JSON响应,并使用ѭ10填充表格行   http://jsbin.com/umuri5/edit     ,        两个薄: 1)返回的json格式不正确。由于某种原因,userdata元素以\“ \\ n为前缀,因此使userdata元素不可用, 2)您应该使用data.results [0] .userdata遍历每个用户。即:
$.each(data.results[0].userdata,function(i,user){
        var tblRow =
                \"<tr>\"
                +\"<td>\"+user.first+\"</td>\"
                +\"<td>\"+user.last+\"</td>\"
                +\"<td>\"+user.email+\"</td>\"
                +\"<td>\"+user.city+\"</td>\"
                +\"</tr>\"
        $(tblRow).appendTo(\"#userdata tbody\");
});