javascript - 如何浏览这个 JSON 对象并获取它的值?

标签 javascript jquery html json

我有一个 JSON 数组(对象),我想浏览它并获取它的值。但我得到:“未捕获的类型错误:无法读取未定义的属性‘代码’”。

这是数组:

{
    "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 View ,我想在表中显示数组的值。我这样做是这样的:

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>';

            };

这是显示调试器的错误:

Console Error

请帮忙。谢谢

最佳答案

"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/

相关文章:

javascript - 迭代DataTable()以获取每一行的td值

c# - 禁用报告按钮链接不想禁用 - html5

javascript - 如何使导航栏滚动并在展开时固定到页面顶部?

jQuery 添加 Year) 至今

javascript - 清除 html 输入保留(ctrl+z)

php - 我可以一次性显示html表格中的一百万条记录吗?

javascript - 如何更改函数以将其操作限制为 id、类或选择器

html - 悬停效果使导航栏过渡的背景矩形向上

javascript - Rxjs如何重用运算符

javascript - 使用 JQUERY 选择的 cb 列表的复选框过滤