我有一个JSON数组(对象),我想通过它来导航并获得它的值。但是我得到了:" Uncaught TypeError:无法读取属性'代码'未定义"。
这是数组:
{
"producto": [
[
{
"id": "1087",
"code": "119025",
"code2": "9025",
"name": "S22.ARABE MEDITERRANEO SANDWICH "
}
]
],
"componentes": [
[
{
"id": "1759",
"code": "31037025",
"code2": "31750",
"name": "TOMATE SECO X KL",
"quantity": "0.025",
"costo": "0.00000000000000000000"
}
],
[
{
"id": "1691",
"code": "31032006",
"code2": "7792070010548",
"name": "31201.ACEITUNAS VERDES EN RODAJASX KL",
"quantity": "0.030",
"costo": "0.00000000000000000000"
}
],
[
{
"id": "1724",
"code": "31042028",
"code2": "33211",
"name": "LECHUGA X KILO",
"quantity": "0.010",
"costo": "0.00000000000000000000"
}
],
[
{
"id": "1741",
"code": "31062014",
"code2": "34210",
"name": "QUESO MOZZARELLA X KL",
"quantity": "0.075",
"costo": "0.00000000000000000000"
}
],
[
{
"id": "435",
"code": "111095",
"code2": "1095",
"name": "PAN ARABE BLANCO X KILO",
"quantity": "0.160",
"costo": "2653.0000000000000000"
}
],
[
{
"id": "1742",
"code": "31062004",
"code2": "34208",
"name": "QUESO POLENGY X KL",
"quantity": "0.050",
"costo": "0.00000000000000000000"
}
],
[
{
"id": "1719",
"code": "31016006",
"code2": "32602",
"name": "JAMONADA X KL",
"quantity": "0.050",
"costo": "0.00000000000000000000"
}
],
[
{
"id": "1694",
"code": "31042005",
"code2": "33203",
"name": "ALBAHACA X KL",
"quantity": "0.005",
"costo": "0.00000000000000000000"
}
]
]
}
我有一个html视图,我想在表中显示数组的值。我是这样做的:
var html = '';
html += '<h2>';
html += 'Costo de Produccion';
html += '</h2>';
html += '<table id="items" cellpadding="0" cellspacing="0" style="width:500px;">'
html += '<tr>';
html += '<th>Codigo</th>';
html += '<th>Codigo Secundario</th>';
html += '<th>Producto</th>';
html += '<th>Cantidad</th>';
html += '<th>Costo</th>';
html += '</tr>';
html += '<tr>';
html += '<td class="code">';
html += '<input type="hidden" value="" class="id">';
html += producto['producto']['code'];
html += ' ';
html += '</td>';
html += '<td class="code2">';
html += producto['producto']['code2'];
html += ' ';
html += '</td>';
html += '<td class="name">';
html += producto['producto']['name'];
html += ' ';
html += '</td>';
html += '<td class="quantity">';
html += cantidad;
html += ' ';
html += '</td>';
html += '</tr>';
html += '</table>';
html += '<h2 style="padding-top: 50px;">';
html += 'Materia Prima Necesaria';
html += '</h2>';
html += '<table id="items" cellpadding="0" cellspacing="0" style="width:500px;">'
html += '<tr>';
html += '<th>Codigo</th>';
html += '<th>Codigo Secundario</th>';
html += '<th>Componente</th>';
html += '<th>Cantidad</th>';
html += '<th>Costo</th>';
html += '</tr>';
for (var i = 0; i < producto['componentes'].length; i++)
{
html += '<tr>';
html += '<td class="code">';
html += '<input type="hidden" value="" class="id">';
html += producto[i]['code'];
html += ' ';
html += '</td>';
html += '<td class="code2">';
html += producto[i]['code2'];
html += ' ';
html += '</td>';
html += '<td class="name">';
html += producto[i]['name'];
html += ' ';
html += '</td>';
html += '<td class="quantity">';
html += cantidad * producto[i]['quantity'];
html += ' ';
html += '</td>';
// html += '<td class="cost">';
// html += cost;
// html += ' ';
// html += '</td>';
html += '</tr>';
html += '</table>';
};
这是显示调试器的错误:
请帮忙。感谢
答案 0 :(得分:2)
"producto": [
[
{
"id": "1087",
"code": "119025",
"code2": "9025",
"name": "S22.ARABE MEDITERRANEO SANDWICH "
}
]
],
producto
是一个数组,其中一个数组包含一个项目。应该是producto['producto'][0][0]['name']
。