jQuery如何检索和提醒JSON数据

时间:2017-10-14 07:01:08

标签: javascript jquery json

JSON:

[  
   {  
      "ID":"25",
      "Serial":"1",
      "Purchase_id":"8",
      "Item":"23",
      "Unit":"1",
      "HSN":"84212120",
      "Quantity":"10",
      "Purchase_rate":"100",
      "Discount":"10",
      "Discount_2":"5",
      "Net_rate":"85.5",
      "CGST_Percentage":"9",
      "SGST_Percentage":"9",
      "IGST_Percentage":"0",
      "Rate_after_tax":"100.89",
      "CGST":"76.95",
      "SGST":"76.95",
      "IGST":"0",
      "Net_amount_without_tax":"855",
      "Net_amount":"1008.9"
   }
]

jQuery的:

$.ajax({
    method: "POST",
    url: formsubmission,
    data: data,
    success: function(response) {
        var data = JSON.parse(response);
        alert(data.ID);
    }
})

任何人都可以帮助我,为什么警报会带来未定义的消息。如何解决i8t问题?如何提醒25而不是未定义?

2 个答案:

答案 0 :(得分:1)

对象位于已解析响应的结果0的索引Array处。您可以使用括号表示法来引用JavaScript对象<{p>}的索引0处的元素

let response =`[{"ID":"25","Serial":"1","Purchase_id":"8","Item":"23","Unit":"1","HSN":"84212120","Quantity":"10","Purchase_rate":"100","Discount":"10","Discount_2":"5","Net_rate":"85.5","CGST_Percentage":"9","SGST_Percentage":"9","IGST_Percentage":"0","Rate_after_tax":"100.89","CGST":"76.95","SGST":"76.95","IGST":"0","Net_amount_without_tax":"855","Net_amount":"1008.9"}]`;

let data = JSON.parse(response);

alert(data[0].ID);

答案 1 :(得分:0)

&#13;
&#13;
var response =`[{"ID":"25","Serial":"1","Purchase_id":"8","Item":"23","Unit":"1","HSN":"84212120","Quantity":"10","Purchase_rate":"100","Discount":"10","Discount_2":"5","Net_rate":"85.5","CGST_Percentage":"9","SGST_Percentage":"9","IGST_Percentage":"0","Rate_after_tax":"100.89","CGST":"76.95","SGST":"76.95","IGST":"0","Net_amount_without_tax":"855","Net_amount":"1008.9"}]`;

    var data = JSON.parse(response);
    for (var i = 0; i < data.length; i++) {    
      alert(data[i].ID);
    } 
&#13;
&#13;
&#13;