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

发布于 2024-11-15 03:04:47 字数 2790 浏览 3 评论 0原文

我想使用 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>

cross-domain-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.');
    }
}

我想在表中显示未格式化的数据?如何 ? 请给出解决方案,问题在哪里或缺少什么?

提前致谢 !! :)

i want to parse remote JSON file using JQuery, YQL(you know cross domain proble, so yql is best)

but i dont know what is misssing in this code ?
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>

cross-domain-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.');
    }
}

i want to display unformatted data in to table ? how ?
plese give solution where is the probleam or what is missing ?

thanks in advance !! :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

音栖息无 2024-11-22 03:04:47

我想使用 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 响应并使用 < 填充表行代码>jQuery.template()

http://jsbin.com/umuri5/edit

i want to parse remote JSON file using JQuery, YQL

You say that you want to parse some JSON, but your YQL query asks for HTML and the YQL URL asks for an XML response!

var yql = 'http://query.yahooapis.com/v1/public/yql?q=' + encodeURIComponent('select * from html where url="' + site + '"') + '&format=xml&callback=?';

If you really want to work with JSON, change that line to something like below. It a) uses the json table (since that is the nature of the content on the site) and b) tells YQL to return JSON because you're using the jQuery.getJSON() function!

var yql = 'http://query.yahooapis.com/v1/public/yql?'
        + 'q=' + encodeURIComponent('select * from json where url=@url')
        + '&url=' + encodeURIComponent(site)
        + '&format=json&callback=?';

Now that YQL returns JSON, you can get at the json object via data.query.results.json which then contains an array of userdata objects. See a fuller example, based on your code, which takes the JSON response from YQL and populates the table rows using jQuery.template()

http://jsbin.com/umuri5/edit

左岸枫 2024-11-22 03:04:47

有两点:

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");
});

Two thins:

1)The json being returned in not well formed. For some reason the userdata element is prefixed with "\n

, hence making the element userdata unavailable,

2)You should be using data.results[0].userdata to iterate over each user. i.e:

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