2015-07-02 32 views
0

我有一個JSON數組(對象),我想導航它並獲取它的值。但我得到:「Uncaught TypeError:無法讀取未定義的屬性'代碼'」。如何瀏覽這個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視圖,我要顯示在一個表中的數組的值。我做這樣的:

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

請幫助。謝謝

+0

'json array'被稱爲對象。 – depperm

+0

謝謝,我糾正了 – juanpscotto

+0

你的json obj叫什麼? – depperm

回答

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

producto是一個數組與一個項目的數組。應該是producto['producto'][0][0]['name']