我有一个JSON数组(对象),我想浏览它并获取它的值。但是我得到了:“未捕获的TypeError:无法读取未定义的属性'code'”。

这是数组:

{
    "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 +=         '&nbsp;';
            html +=     '</td>';
            html +=     '<td class="code2">';
            html +=         producto['producto']['code2'];
            html +=         '&nbsp;';
            html +=     '</td>';
            html +=     '<td class="name">';
            html +=         producto['producto']['name'];
            html +=         '&nbsp;';
            html +=     '</td>';
            html +=     '<td class="quantity">';
            html +=         cantidad;
            html +=         '&nbsp;';
            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 +=         '&nbsp;';
                html +=     '</td>';
                html +=     '<td class="code2">';
                html +=         producto[i]['code2'];
                html +=         '&nbsp;';
                html +=     '</td>';
                html +=     '<td class="name">';
                html +=         producto[i]['name'];
                html +=         '&nbsp;';
                html +=     '</td>';
                html +=     '<td class="quantity">';
                html +=         cantidad * producto[i]['quantity'];
                html +=         '&nbsp;';
                html +=     '</td>';
                // html +=  '<td class="cost">';
                // html +=      cost;
                // html +=      '&nbsp;';
                // html +=  '</td>';
                html += '</tr>';
                html += '</table>';

            };


这是显示调试器的错误:



请帮忙。谢谢

最佳答案

"producto": [
    [
        {
            "id": "1087",
            "code": "119025",
            "code2": "9025",
            "name": "S22.ARABE MEDITERRANEO SANDWICH "
        }
    ]
],


producto是一个数组,其中一个数组包含一项。应该是producto['producto'][0][0]['name']

关于javascript - 如何浏览此JSON对象并获取其值?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31193102/

10-09 09:35