从JSON对象获取数据

时间:2022-12-04 16:01:13

I'm working on my first (EVER!) JSON project, and I'm wondering how to get data from a JSON object. I've imported it in, and what I need to do is get the input from the value field and compare it to an input the user has entered. Unfortunately, I can't even figure out how to get data from the JSON object. It seems to be loading successfully, but I can't reference it. Here' s a sample of the JSON I'm loading:

我正在研究我的第一个(EVER!)JSON项目,我想知道如何从JSON对象获取数据。我已将其导入,我需要做的是从值字段获取输入并将其与用户输入的输入进行比较。不幸的是,我甚至无法弄清楚如何从JSON对象获取数据。它似乎正在加载成功,但我无法引用它。这是我正在加载的JSON示例:

{
    "films": [{
        "label": "34",
        "value": "34",
        "currently_streaming": "1",
        "full_streaming_url": "http://www.url.com",
        "url": "http://www.url.com"},
    {
        "label": "A Different Color",
        "value": "A Different Color",
        "currently_streaming": "1",
        "full_streaming_url": "http://www.url.php",
        "url": "http://www.url.com"}]
}​

And here's the code that loads it. It returns as successful, but I can't get any of the data from the JSON object:

这是加载它的代码。它返回成功,但我无法从JSON对象获取任何数据:

 $(document).ready(function(){
    $.ajax({
        dataType: 'json',
        beforeSend : function() {
           console.log('Before Ajax Request Starts !!');
        },
        success: function(data) {
            console.log(data);
            alert("Edddddddd");
            $.each(json.films, function(i, object) {
            $.each(object, function(property, value) {
            alert(property + "=" + value);
    });
});
        },
        error : function(jqXHR, textStatus, errorThrown) {
             alert("Error occurred: " + errorThrown);
        },
         beforeSend : function() {
           console.log('Ajax Request Complete  !!');
        },
        url: 'test.php'
    });  
});

I clearly have no idea what I'm doing, and so would really appreciate any help!

我显然不知道我在做什么,所以非常感谢任何帮助!

1 个解决方案

#1


5  

Change from:

改变自:

$.each(json.films, function(i, object)

to:

至:

$.each(data.films, function(i, object)

#1


5  

Change from:

改变自:

$.each(json.films, function(i, object)

to:

至:

$.each(data.films, function(i, object)