JSON每个循环都不起作用。怎么解决?

时间:2017-10-31 14:52:15

标签: jquery json

jQuery的:

var data = JSON.parse(response);
$.each(data, function(i, item) {
    alert(data[i].ID);
});

回应JSON:

[  
   {  
      "ID":"33",
      "Serial":"1",
      "Purchase_id":"9",
      "Item":"ds 18 sticker",
      "Unit":"Piece",
      "HSN":"84212120",
      "Quantity":"110",
      "Purchase_rate":"85",
      "Discount":"0",
      "Discount_2":"0",
      "Net_rate":"85",
      "CGST_Percentage":"9",
      "SGST_Percentage":"9",
      "IGST_Percentage":"0",
      "Rate_after_tax":"100.3",
      "CGST":"841.5",
      "SGST":"841.5",
      "IGST":"0",
      "Net_amount_without_tax":"9350",
      "Net_amount":"11033",
      "Item_id":"25"
   },
   {  
      "ID":"42",
      "Serial":"1",
      "Purchase_id":"16",
      "Item":"ds 18 sticker",
      "Unit":"Piece",
      "HSN":"84212120",
      "Quantity":"1000",
      "Purchase_rate":"10",
      "Discount":"0",
      "Discount_2":"0",
      "Net_rate":"10",
      "CGST_Percentage":"9",
      "SGST_Percentage":"9",
      "IGST_Percentage":"0",
      "Rate_after_tax":"11.8",
      "CGST":"900",
      "SGST":"900",
      "IGST":"0",
      "Net_amount_without_tax":"10000",
      "Net_amount":"11800",
      "Item_id":"25"
   }
]

任何人都可以告诉我它出现错误的原因Uncaught SyntaxError:无效或意外的令牌。我该如何解决?通过使用每个循环我如何获取数据?抱歉我的英语不好。提前致谢。请编辑此问题以便其他人可以帮助。

2 个答案:

答案 0 :(得分:1)

对我来说很好。

var data = [  
   {  
      "ID":"33",
      "Serial":"1",
      "Purchase_id":"9",
      "Item":"ds 18 sticker",
      "Unit":"Piece",
      "HSN":"84212120",
      "Quantity":"110",
      "Purchase_rate":"85",
      "Discount":"0",
      "Discount_2":"0",
      "Net_rate":"85",
      "CGST_Percentage":"9",
      "SGST_Percentage":"9",
      "IGST_Percentage":"0",
      "Rate_after_tax":"100.3",
      "CGST":"841.5",
      "SGST":"841.5",
      "IGST":"0",
      "Net_amount_without_tax":"9350",
      "Net_amount":"11033",
      "Item_id":"25"
   },
   {  
      "ID":"42",
      "Serial":"1",
      "Purchase_id":"16",
      "Item":"ds 18 sticker",
      "Unit":"Piece",
      "HSN":"84212120",
      "Quantity":"1000",
      "Purchase_rate":"10",
      "Discount":"0",
      "Discount_2":"0",
      "Net_rate":"10",
      "CGST_Percentage":"9",
      "SGST_Percentage":"9",
      "IGST_Percentage":"0",
      "Rate_after_tax":"11.8",
      "CGST":"900",
      "SGST":"900",
      "IGST":"0",
      "Net_amount_without_tax":"10000",
      "Net_amount":"11800",
      "Item_id":"25"
   }
];

$.each(data, function(index, item){ console.log(item.ID); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

答案 1 :(得分:0)

您可以使用解决方案

&#13;
&#13;
var data = [  
   {  
      "ID":"33",
      "Serial":"1",
      "Purchase_id":"9",
      "Item":"ds 18 sticker",
      "Unit":"Piece",
      "HSN":"84212120",
      "Quantity":"110",
      "Purchase_rate":"85",
      "Discount":"0",
      "Discount_2":"0",
      "Net_rate":"85",
      "CGST_Percentage":"9",
      "SGST_Percentage":"9",
      "IGST_Percentage":"0",
      "Rate_after_tax":"100.3",
      "CGST":"841.5",
      "SGST":"841.5",
      "IGST":"0",
      "Net_amount_without_tax":"9350",
      "Net_amount":"11033",
      "Item_id":"25"
   },
   {  
      "ID":"42",
      "Serial":"1",
      "Purchase_id":"16",
      "Item":"ds 18 sticker",
      "Unit":"Piece",
      "HSN":"84212120",
      "Quantity":"1000",
      "Purchase_rate":"10",
      "Discount":"0",
      "Discount_2":"0",
      "Net_rate":"10",
      "CGST_Percentage":"9",
      "SGST_Percentage":"9",
      "IGST_Percentage":"0",
      "Rate_after_tax":"11.8",
      "CGST":"900",
      "SGST":"900",
      "IGST":"0",
      "Net_amount_without_tax":"10000",
      "Net_amount":"11800",
      "Item_id":"25"
   }
]

$.each(data, function(i, item) {
    console.log(data[i].ID);
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;
&#13;
&#13;