[英]Iterate over this JSON object and get it's values?
我有一个 JSON 数组(对象),我想浏览它并获取它的值。 但我得到:“未捕获的类型错误:无法读取未定义的属性‘长度’”。
这是生成 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 += ' ';
html += '</td>';
html += '<td class="code2">';
html += producto[i]["Motion"]["code2"];
html += ' ';
html += '</td>';
html += '<td class="name">';
html += producto[i]["Motion"]["name"];
html += ' ';
html += '</td>';
html += '<td class="quantity">';
html += producto[i]["Motion"]["quantity"];
html += ' ';
html += '</td>';
html += '</tr>';
};
};
html += '</table>';
);
这是显示调试器的错误:
第一个 console.log 是返回的名为 producto 的 json 的 console.log()
请你帮帮我。 谢谢
我在 Jsfiddle 中使用了 Javascript 和您的 html,它可以正常工作。 链接到工作代码
所以我认为还有其他事情我猜一些变量是未定义的。
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 += ' ';
html += '</td>';
html += '<td class="code2">';
html += producto[i]["Motion"]["code2"];
html += ' ';
html += '</td>';
html += '<td class="name">';
html += producto[i]["Motion"]["name"];
html += ' ';
html += '</td>';
html += '<td class="quantity">';
html += producto[i]["Motion"]["quantity"];
html += ' ';
html += '</td>';
html += '</tr>';
};
};
html += '</table>';
alert(html);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.