0

jQuery:

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

response 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"
   }
]

Anyone can please tell me why it appearing error Uncaught SyntaxError: Invalid or unexpected token. How can I resolve it? and by using each loop how can I get data? sorry for my weak English. Thanks in advance. please edit this question so it can help others.

3
  • 2
    The each is passing in the index and the element at that index. Why are you accessing data[i] instead of item? Commented Oct 31, 2017 at 14:54
  • Invalid or unexpected token may be pointing out invalid json for the parse of the json. Commented Oct 31, 2017 at 14:58
  • alert(item.ID) should work Commented Oct 31, 2017 at 14:58

2 Answers 2

1

Works fine for me.

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>

Sign up to request clarification or add additional context in comments.

Comments

0

Here you go with a solution

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);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.