Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/79.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
Javascript 迭代这个JSON对象并获取它的值?_Javascript_Jquery_Json - Fatal编程技术网

Javascript 迭代这个JSON对象并获取它的值?

Javascript 迭代这个JSON对象并获取它的值?,javascript,jquery,json,Javascript,Jquery,Json,我有一个JSON数组对象,我想通过它导航并获取它的值。但我得到:uncaughttypeerror:无法读取未定义的属性“length” 这是生成JSON的ajax函数: $.ajax({ type: 'GET', url: "<?php echo $html->url(array("controller" => "porders", "action" => "getBudgetProduct")); ?>", data: "product_id="+p

我有一个JSON数组对象,我想通过它导航并获取它的值。但我得到:uncaughttypeerror:无法读取未定义的属性“length”

这是生成JSON的ajax函数:

$.ajax({
  type: 'GET',
  url: "<?php echo $html->url(array("controller" => "porders", "action" => "getBudgetProduct")); ?>",
  data: "product_id="+product_id+"&cantidad="+cantidad,
  dataType:"text json",
  success: function(producto){
    console.log(producto);
    addCost(producto, cantidad);

  },
  error: function() {
    console.log("Error en el AJAX");
  }
});
我有一个html视图,我想在表中显示数组的值。我就是这样做的。addCost函数代码:

function addCost(producto, cantidad)
{
    var html = '';
                html += '<h2>';
                html += '<?php  __("Costo de Producción");?>';
                html += '</h2>';
                html += '<h3>';
                html += '<?php  __("Lista de Semiterminados");?>';
                html += '</h3>';
                html += '<table id="items" cellpadding="0" cellspacing="0" style="width:500px;">'
                html += '<tr>';
                html +=     '<th><?php __("Codigo"); ?></th>';
                html +=     '<th><?php __("Codigo Secundario"); ?></th>';
                html +=     '<th><?php __("Producto"); ?></th>';
                html +=     '<th><?php __("Cantidad"); ?></th>';
                html += '</tr>';
                for (var i = 0; i < producto.length; i++) {
                    if (producto[i]["Motion"]["composers"] > 0) 
                    {
                        html += '<tr>';
                        html +=     '<td class="code">';
                        html +=         '<input type="hidden" value="" class="id">';
                        html +=         producto[i]["Motion"]["code"];
                        html +=         '&nbsp;';
                        html +=     '</td>';
                        html +=     '<td class="code2">';
                        html +=         producto[i]["Motion"]["code2"];
                        html +=         '&nbsp;';
                        html +=     '</td>';
                        html +=     '<td class="name">';
                        html +=         producto[i]["Motion"]["name"];
                        html +=         '&nbsp;';
                        html +=     '</td>';
                        html +=     '<td class="quantity">';
                        html +=         producto[i]["Motion"]["quantity"];
                        html +=         '&nbsp;';
                        html +=     '</td>';
                        html += '</tr>';    
                    };
                };
                html += '</table>';
);
这是显示调试器的错误:

第一个console.log是返回的名为producto的json的console.log


卡尔,请你帮帮我。谢谢

我把Javascript和html都带到了JSFIDLE,它们在这里工作正常

所以我想还有一些其他的东西,我想一些变量是未定义的

var producto=[
    {
        "Motion": {
            "id": "1801",
            "code": "31042014",
            "code2": "33207",
            "name": "CEBOLLAS X KILO",
            "composers": "0",
            "buy_price": "7400",
            "composition_cost": "0.00000000000000000000",
            "quantity": "7.5"
        }
    },
    {
        "Motion": {
            "id": "1912",
            "code": "31061009",
            "code2": "3134108",
            "name": "LECHE ENTERA EN SACHET X LT",
            "composers": "0",
            "buy_price": "3100",
            "composition_cost": "0.00000000000000000000",
            "quantity": "6.0"
        }
    },
    {
        "Motion": {
            "id": "1718",
            "code": "31034001",
            "code2": "31401",
            "name": "HUEVOS X UNIDAD",
            "composers": "0",
            "buy_price": "433.333333",
            "composition_cost": "0.00000000000000000000",
            "quantity": "50"
        }
    },
    {
        "Motion": {
            "id": "300001091",
            "code": "31053003",
            "code2": "35305",
            "name": "HARINA DE MAIZ X KGR",
            "composers": "0",
            "buy_price": "5000",
            "composition_cost": "0.00000000000000000000",
            "quantity": "4.500"
        }
    },
    {
        "Motion": {
            "id": "1748",
            "code": "31062016",
            "code2": "34202",
            "name": "QUESO PARAGUAY X KL",
            "composers": "0",
            "buy_price": "25000",
            "composition_cost": "0.00000000000000000000",
            "quantity": "4.500"
        }
    },
    {
        "Motion": {
            "id": "1775",
            "code": "31031005",
            "code2": "31101",
            "name": "ACEITE  A GRANEL X LT",
            "composers": "0",
            "buy_price": "7299.6",
            "composition_cost": "0.00000000000000000000",
            "quantity": "4.500"
        }
    },
    {
        "Motion": {
            "id": "1752",
            "code": "31035047",
            "code2": "31501",
            "name": "SAL FINA X KL",
            "composers": "0",
            "buy_price": "2037.98",
            "composition_cost": "0.00000000000000000000",
            "quantity": "0.225"
        }
    },
    {
        "Motion": {
            "id": "300001800",
            "code": "210402001",
            "code2": "84231",
            "name": "SOPA PARAGUAYA X KILO",
            "composers": "7",
            "buy_price": "0",
            "composition_cost": "10778.50661836734693877551020",
            "quantity": "7.5"
        }
    }
];

var html = '';
            html += '<h2>';
            html += '<?php  __("Costo de Producción");?>';
            html += '</h2>';
            html += '<h3>';
            html += '<?php  __("Lista de Semiterminados");?>';
            html += '</h3>';
            html += '<table id="items" cellpadding="0" cellspacing="0" style="width:500px;">'
            html += '<tr>';
            html +=     '<th><?php __("Codigo"); ?></th>';
            html +=     '<th><?php __("Codigo Secundario"); ?></th>';
            html +=     '<th><?php __("Producto"); ?></th>';
            html +=     '<th><?php __("Cantidad"); ?></th>';
            html += '</tr>';
            for (var i = 0; i < producto.length; i++) {
                if (producto[i]["Motion"]["composers"] > 0) 
                {
                    html += '<tr>';
                    html +=     '<td class="code">';
                    html +=         '<input type="hidden" value="" class="id">';
                    html +=         producto[i]["Motion"]["code"];
                    html +=         '&nbsp;';
                    html +=     '</td>';
                    html +=     '<td class="code2">';
                    html +=         producto[i]["Motion"]["code2"];
                    html +=         '&nbsp;';
                    html +=     '</td>';
                    html +=     '<td class="name">';
                    html +=         producto[i]["Motion"]["name"];
                    html +=         '&nbsp;';
                    html +=     '</td>';
                    html +=     '<td class="quantity">';
                    html +=         producto[i]["Motion"]["quantity"];
                    html +=         '&nbsp;';
                    html +=     '</td>';
                    html += '</tr>';    
                };
            };
            html += '</table>';
alert(html);

显示您的javascript代码!调试器告诉您,问题出在第1582行的javascript文件预算中。从哪个对象调用addCost?在那一点上它可能是空的吗?是您从addCost函数提供的代码吗?producto来自哪里?producto是什么?我打赌它不是数组。错误表明它实际上是未定义的。对不起,我将更新它,这是来自addCost函数的代码这更适合于注释-这不是真正的答案。您认为问题是什么?我可以使用以下语法:producto[i].Motion.quantity而不是此producto[i][Motion][quantity]有什么区别吗?我在函数的底部声明了一个未定义的变量我让代码正常工作了!!它应该给他洞察力。。。我认为他的问题并不是很严重。。。
var producto=[
    {
        "Motion": {
            "id": "1801",
            "code": "31042014",
            "code2": "33207",
            "name": "CEBOLLAS X KILO",
            "composers": "0",
            "buy_price": "7400",
            "composition_cost": "0.00000000000000000000",
            "quantity": "7.5"
        }
    },
    {
        "Motion": {
            "id": "1912",
            "code": "31061009",
            "code2": "3134108",
            "name": "LECHE ENTERA EN SACHET X LT",
            "composers": "0",
            "buy_price": "3100",
            "composition_cost": "0.00000000000000000000",
            "quantity": "6.0"
        }
    },
    {
        "Motion": {
            "id": "1718",
            "code": "31034001",
            "code2": "31401",
            "name": "HUEVOS X UNIDAD",
            "composers": "0",
            "buy_price": "433.333333",
            "composition_cost": "0.00000000000000000000",
            "quantity": "50"
        }
    },
    {
        "Motion": {
            "id": "300001091",
            "code": "31053003",
            "code2": "35305",
            "name": "HARINA DE MAIZ X KGR",
            "composers": "0",
            "buy_price": "5000",
            "composition_cost": "0.00000000000000000000",
            "quantity": "4.500"
        }
    },
    {
        "Motion": {
            "id": "1748",
            "code": "31062016",
            "code2": "34202",
            "name": "QUESO PARAGUAY X KL",
            "composers": "0",
            "buy_price": "25000",
            "composition_cost": "0.00000000000000000000",
            "quantity": "4.500"
        }
    },
    {
        "Motion": {
            "id": "1775",
            "code": "31031005",
            "code2": "31101",
            "name": "ACEITE  A GRANEL X LT",
            "composers": "0",
            "buy_price": "7299.6",
            "composition_cost": "0.00000000000000000000",
            "quantity": "4.500"
        }
    },
    {
        "Motion": {
            "id": "1752",
            "code": "31035047",
            "code2": "31501",
            "name": "SAL FINA X KL",
            "composers": "0",
            "buy_price": "2037.98",
            "composition_cost": "0.00000000000000000000",
            "quantity": "0.225"
        }
    },
    {
        "Motion": {
            "id": "300001800",
            "code": "210402001",
            "code2": "84231",
            "name": "SOPA PARAGUAYA X KILO",
            "composers": "7",
            "buy_price": "0",
            "composition_cost": "10778.50661836734693877551020",
            "quantity": "7.5"
        }
    }
];

var html = '';
            html += '<h2>';
            html += '<?php  __("Costo de Producción");?>';
            html += '</h2>';
            html += '<h3>';
            html += '<?php  __("Lista de Semiterminados");?>';
            html += '</h3>';
            html += '<table id="items" cellpadding="0" cellspacing="0" style="width:500px;">'
            html += '<tr>';
            html +=     '<th><?php __("Codigo"); ?></th>';
            html +=     '<th><?php __("Codigo Secundario"); ?></th>';
            html +=     '<th><?php __("Producto"); ?></th>';
            html +=     '<th><?php __("Cantidad"); ?></th>';
            html += '</tr>';
            for (var i = 0; i < producto.length; i++) {
                if (producto[i]["Motion"]["composers"] > 0) 
                {
                    html += '<tr>';
                    html +=     '<td class="code">';
                    html +=         '<input type="hidden" value="" class="id">';
                    html +=         producto[i]["Motion"]["code"];
                    html +=         '&nbsp;';
                    html +=     '</td>';
                    html +=     '<td class="code2">';
                    html +=         producto[i]["Motion"]["code2"];
                    html +=         '&nbsp;';
                    html +=     '</td>';
                    html +=     '<td class="name">';
                    html +=         producto[i]["Motion"]["name"];
                    html +=         '&nbsp;';
                    html +=     '</td>';
                    html +=     '<td class="quantity">';
                    html +=         producto[i]["Motion"]["quantity"];
                    html +=         '&nbsp;';
                    html +=     '</td>';
                    html += '</tr>';    
                };
            };
            html += '</table>';
alert(html);