Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 现在我实际上可以得到这个JSON数据了,我如何使用它/循环它呢?_Jquery_Ajax_Json - Fatal编程技术网

Jquery 现在我实际上可以得到这个JSON数据了,我如何使用它/循环它呢?

Jquery 现在我实际上可以得到这个JSON数据了,我如何使用它/循环它呢?,jquery,ajax,json,Jquery,Ajax,Json,我正在进行我的第一个(有史以来!)JSON项目,我想知道如何从JSON对象获取特定数据。我已经导入了它,刚刚发现(谢谢堆栈溢出!)如何引用该数据。我现在能够做到这一点,下面的代码是我所能得到的。我有一大堆通过数据的警报。我现在需要做的实际上是循环并计算JSON的值字段,如下所示。我不知道如何选择它,以及如何循环它。我尝试过data.films.value.value和data.films.value.value[0]之类的方法,我确信这与此非常接近,但我不知道如何将数据转换成一种形式,让我可以像

我正在进行我的第一个(有史以来!)JSON项目,我想知道如何从JSON对象获取特定数据。我已经导入了它,刚刚发现(谢谢堆栈溢出!)如何引用该数据。我现在能够做到这一点,下面的代码是我所能得到的。我有一大堆通过数据的警报。我现在需要做的实际上是循环并计算JSON的值字段,如下所示。我不知道如何选择它,以及如何循环它。我尝试过data.films.value.value和data.films.value.value[0]之类的方法,我确信这与此非常接近,但我不知道如何将数据转换成一种形式,让我可以像普通字符串一样使用特定元素。我最终要做的是将每个films.value元素与用户输入的字符串进行比较,如果无法获取该元素,我不确定如何进行比较

{
    "

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"}]
}​
这是加载它的代码。它返回为成功,但我无法从JSON对象获取任何数据:

 $(document).ready(function(){
    $.ajax({
        dataType: 'json',
        beforeSend : function() {
           console.log('Before Ajax Request Starts !!');
        },
        success: function(data) {
            console.log(data);
            alert("Edddddddd");
            $.each(data.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'
    });  
});
非常感谢您的任何帮助

更新 我现在正试着用这里每个人给我的建议来循环。这就是我现在拥有的:

    var test="34x25x36";
 $(document).ready(function(){
    $.ajax({
        dataType: 'json',
        beforeSend : function() {
           console.log('Before Ajax Request Starts !!');
        },
        success: function(data) {
            console.log(data);
            alert("Edddddddd");
            $.each(data.films, function(i, object) {
            $.each(object, function(property, value) {
            //alert(property + "=" + value);
            for (i=0; i<data.films.length; i++){
            var theFilm = (data.films[i].value);
            if (theFilm === test){
            document.write("Your movie is hot");
            document.write(theFilm);
            }
            else {
            }
            }
    });
});
        },
        error : function(jqXHR, textStatus, errorThrown) {
             alert("Error occurred: " + errorThrown);
        },
         beforeSend : function() {
           console.log('Ajax Request Complete  !!');
        },
        url: 'test.php'
    });  
});
var test=“34x25x36”;
$(文档).ready(函数(){
$.ajax({
数据类型:“json”,
beforeSend:function(){
log('在Ajax请求启动之前!!');
},
成功:功能(数据){
控制台日志(数据);
警报(“EDDDDD”);
$。每个(数据、胶片、功能(i、对象){
$.each(对象、函数(属性、值){
//警报(属性+“=”+值);
对于(i=0;iTry

电影[0]。标签将为您提供34张

要进行迭代,您需要使用

$.each(data.films, function(i, object) { 
// And Not 
$.each(json.films, function(i, object) {
试试这个

更新代码:

因为每个数组项都是一个对象,所以需要再写一个循环来访问其中的内容

$.each(data.films, function() {
    $.each(this, function(j, val) {
        console.log('Key - ' + j + ' :: Value - ' + val);
    });
});​
这在你的情况下应该有效

function(data) {
    console.log(data);
    alert("Edddddddd");
    $.each(data.films, function(i, object) {
        $.each(object, function(property, value) {
            alert(property + "=" + value);
        });
    });
}​
//用于循环

var keys = ["label", "value", "currently_streaming", "full_streaming_url", "url"]
for (i = 0; i < data.films.length; i++) {
    for (j = 0; j < keys.length; j++) {
        alert( 'Key - ' + keys[j] + ' :: Value - ' + data.films[i][keys[j]] )  ;
    }
}​
var keys=[“标签”、“值”、“当前\u流媒体”、“完整\u流媒体\u url”、“url”]
对于(i=0;i
检查并尝试以下操作:

success: function(data) {
  for (var item in data.films) {
    alert(item.label);
  }
}

您尝试循环使用json.films而不是data.films

{
{
    "films": [{  // <-- to access this data.films
        "label": "34",  // to access this data.films[0].label - this is the first object in the array
        "value": "34",  // to access this data.films[0].value
        "currently_streaming": "1", //  // to access this data.films[0].currently_streaming
        "full_streaming_url": "http://www.url.com", // and so on
        "url": "http://www.url.com"} // and so on
    {
        "label": "A Different Color", // to access this data.films[1].label - this is the second object in the array
        // arrays are zero indexed
        "value": "A Different Color", // to access this data.films[1].value
        "currently_streaming": "1",
        "full_streaming_url": "http://www.url.php",
        "url": "http://www.url.com"}]
}​

“films”:[{//Ugh,当我写alert(films[0].label)时,这对我来说不起作用;除非我做错了什么(我可能是!),这就是我现在拥有的:success:function(data){console.log(data);alert(“eddddddddd”);$.each(data.films,function(I,object){$.each(object,function)(属性,值){//alert(属性+“=”+value);alert(films[0].label);});应该这样做吗?可以这样做!谢谢!要进行交互,我是否要编写$.each(data.films[I].label)?检查更新的代码…因为每个数组项都是一个对象,您需要再次对其进行迭代..试试这个HMMM,这对我不起作用:$.each(对象、函数(属性、值){//alert(属性+“=”+value);alert($.each(data.films[0].value));});你不是已经问过这个问题了吗?是的!有人教过我如何提取数据,但我不知道如何引用它!嗯,这一直没有定义。你的json名称中可能有新行(如果您的示例与您的代码相匹配)。在json中,尝试使用
“films”:
而不是
“/n/nfilms:“
我的意思是您的引用如何从一行开始,然后有两行空行,然后是
films:”
,\n是编程中新行的表示,现在您可以在任何地方看到它,我确信:)谢谢!我刚刚尝试了一个for循环来循环它们,但它似乎不起作用。for(I=0;I=data.films.length;I++){alert(data.films[I].value);}Change
I=data.films.length
to
我很好用,谢谢!我想不出$。each-我对jQuery也很陌生。我这里的东西行吗?成功:函数(数据){console.log(data);alert(“eddddddddd”);$.each(data.films,function(i,object){$.each(object,function(property,value){//alert(property+“=”+value);for(i=0;iIt)很难回答这个问题,因为注释中没有格式。请编辑到您的问题中。
$.each(data.films, function(k,v){
    console.log(v.value); // <-- this will give you the value for both
    // now that you're here you can get value for each property by 
    // v.label
    // v.value
    // v.currently_streaming
    // v.full_streaming_url
    // v.url
});