2

有人可以帮我用 JQuery 获取 JSON 吗?我想我在我的 JQuery 代码中犯了一些错误。任何帮助将不胜感激。下面是我的 JSON 和 JQuery:

JSON:

{
    "video": [
        {
            "id": "12312412312",
            "name": "Ecuaciones Diferenciales",
            "url": "/video/math/edo/12312412312",
            "author": {
                "data": [
                    {
                        "name_author": "Alejandro Morales",
                        "uri": "/author/alejandro-morales",
                        "type": "master"
                    }
                ]
            },
            "comments": {
                "data": [
                    {
                        "id": "367501354973_12216733",
                        "from": {
                            "name": "Doug Edwards",
                            "id": "628675309"
                        },
                        "message": "Make sure you don't, as they say, go whole hog...\nhttp://www.youtube.com/watch?v=U4wTFuaV8VQ",
                        "created_time": "2010-03-06T03:24:46+0000"
                    },
                    {
                        "id": "367501354973_12249673",
                        "from": {
                            "name": "Tom Taylor",
                            "id": "1249191863"
                        },
                        "message": "Are you and Karen gonna, as they say, pig out?",
                        "created_time": "2010-03-06T21:05:21+0000"
                    },
                    {
                        "id": "367501354973_12249857",
                        "from": {
                            "name": "Sheila Taylor",
                            "id": "1315606682"
                        },
                        "message": "how did it turn out? Sounds nummy!\n",
                        "created_time": "2010-03-06T21:10:30+0000"
                    }
                ]
            }
        },
        {
            "id": "12312412311",
            "name": "Ecuaciones Diferenciales : El arte de las diferenciaciones",
            "url": "/video/math/edo/1231241231212",
            "author": {
                "data": [
                    {
                        "name_author": "Alejandro Morales",
                        "uri": "/author/alejandro-morales",
                        "type": "master"
                    }
                ]
            },
            "comments": {
                "data": [
                    {
                        "id": "367501354973_12216733",
                        "from": {
                            "name": "Doug Edwards",
                            "id": "628675309"
                        },
                        "message": "Make sure you don't, as they say, go whole hog...\nhttp://www.youtube.com/watch?v=U4wTFuaV8VQ",
                        "created_time": "2010-03-06T03:24:46+0000"
                    },
                    {
                        "id": "367501354973_12249673",
                        "from": {
                            "name": "Tom Taylor",
                            "id": "1249191863"
                        },
                        "message": "Are you and Karen gonna, as they say, pig out?",
                        "created_time": "2010-03-06T21:05:21+0000"
                    },
                    {
                        "id": "367501354973_12249857",
                        "from": {
                            "name": "Sheila Taylor",
                            "id": "1315606682"
                        },
                        "message": "how did it turn out? Sounds nummy!\n",
                        "created_time": "2010-03-06T21:10:30+0000"
                    }
                ]
            }
        }
    ]
}

jQuery 代码

var url = 'result.json';
$(document).ready(function() {
    $.getJSON(url, function(data) {
        $.each(data, function(video, data) {
            $('#stage').html('<p> ID:' + data.video + '</p>');
            $('#stage').append('<p Name: ' + data.name+ '</p>');
            $('#stage').append('<p> URL: ' + data.url+ '</p>');

            console.log("========================");
            console.log(data);

        });
    });
});
4

3 回答 3

4

你正在尝试遍历video数组中作为data对象属性的每个项目,因为在你的$.each()循环中你试图访问.video,.name.url属性。所以而不是:

 $.each(data, function(video, data) {

...循环遍历 的每个顶级属性data,请尝试:

 $.each(data.video, function(video, data) {

...循环遍历data.video数组中的项目。

此外,鉴于该回调中的两个参数是当前项目索引和当前项目,我可能会将参数从video和重命名dataiitem。调用不同的变量有点令人困惑data(一个是$.getJSON()回调的参数,另一个是回调的参数$.each())。

于 2012-08-28T04:39:16.813 回答
4

您正在尝试迭代整个 JSON 对象 - 您应该迭代它的一个键。试试这个:

$.each(data.video, function(index, video) {
  $('#stage').append('<p> Name: ' + video.name+ '</p>');
  $('#stage').append('<p> URL: ' + video.url+ '</p>');
});

如果要添加其他数据,可以遍历整个层次结构。例如:

$.each(data.video, function(index, video) {
  $('#stage').append('<p> Name: ' + video.name + '</p>');
  $('#stage').append('<p> URL: ' + video.url + '</p>');

  $.each(video.author.data, function(index, author) {
    $('#stage').append('<p> Author: ' + author.name_author + '</p>');
  });

  $('#stage').append('<br/>');
});

最后,如果你想访问第 n 条记录,使用上面的语法,你可以这样做:

// n is the 0-based position of the json record you're interested in displaying
var video = data.video[n];
$('#stage').append('<p> Name: ' + video.name + '</p>');
$('#stage').append('<p> URL: ' + video.url + '</p>');
// etc...

这里的工作示例:http: //livecoding.io/3495017

于 2012-08-28T04:47:39.647 回答
1

看起来您正在尝试迭代 JSON 的错误部分。我相信你的 $.each 行应该是这样的:

$.each(data.video, function(index, video){
  // video.name, video.url, etc
}
于 2012-08-28T04:39:56.753 回答