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

时间:2015-09-14 19:10:08

标签: javascript jquery json

我有一个JSON数组(对象),我想通过它来导航并获得它的值。但我得到了:" Uncaught TypeError:无法读取属性'长度'未定义"。

这是生成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");
  }
});

这是数组:

[
    {
        "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"
        }
    }
]

我有一个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>';
);

这是显示调试器的错误:

Debugger error

第一个console.log它是返回的json的console.log(),名为producto

请你帮忙。感谢

1 个答案:

答案 0 :(得分:1)

我把Javascript和你的html在Jsfiddle上正常工作了。 Link to working code

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

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);
相关问题