Click here to Skip to main content
15,893,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Below is my JSON data from asp.net web service

d:"[{"ID":"2","Name":"Office Address","Desc":"Office Address","Check":"True","Code":"/^[a-zA-Z0-9\s\:\-]+$","CreatedBy":"felixd","CreatedDateTime":"2/18/2017 8:05:24 PM","UpdatedBy":"","UpdatedDateTime":"","IsActive":"True","ActiveStatus":"Yes","FlagStatus":"Yes"}]"

while trying to iterate the data using below jquery ajax success code
success: function (response) {
  if (response.d != '') {
    var list = JSON.parse(response.d);
    $.each(list, function (k, v) {
      console.log(response.d[k].ID);
    });
   }
},
error: function(response){}

I get the following error
Quote:
Uncaught SyntaxError: Unexpected token s in JSON at position 95
at JSON.parse (<anonymous>)
at Object.success (MasterList.aspx:49)
at fire (jquery-1.12.4.js:3232)
at Object.fireWith [as resolveWith] (jquery-1.12.4.js:3362)
at done (jquery-1.12.4.js:9840)
at XMLHttpRequest.callback (jquery-1.12.4.js:10311)
success @ AddressTypeList.aspx:49
fire @ jquery-1.12.4.js:3232
fireWith @ jquery-1.12.4.js:3362
done @ jquery-1.12.4.js:9840
callback @ jquery-1.12.4.js:10311

how to resolve this error?
Please advise
Thanks

What I have tried:

I tried remove the JSON.parse

I get following error
Uncaught TypeError: Cannot use 'in' operator to search for 'length' in [{"ID":"2","Name":"Office Address","Desc":"Office Address","Check":"True","Code":"/^[a-zA-Z0-9\s\:\-]+$","CreatedBy":"felixd","CreatedDateTime":"2/18/2017 8:05:24 PM","UpdatedBy":"","UpdatedDateTime":"","IsActive":"True","ActiveStatus":"Yes","FlagStatus":"Yes"}]
    at isArrayLike (jquery-1.12.4.js:569)
    at Function.each (jquery-1.12.4.js:367)
    at Object.success (MasterList.aspx:50)
    at fire (jquery-1.12.4.js:3232)
    at Object.fireWith [as resolveWith] (jquery-1.12.4.js:3362)
    at done (jquery-1.12.4.js:9840)
    at XMLHttpRequest.callback (jquery-1.12.4.js:10311)
Posted
Updated 18-Feb-17 7:34am
Comments
Karthik_Mahalingam 18-Feb-17 11:45am    
the given json looks fine.
Christopher Fernandes 18-Feb-17 13:24pm    
it breaks at \s in the regex value. it cant escape the \s

try this
var jsonstr = JSON.stringify(response.d);
var json = JSON.stringify(jsonstr);
 
Share this answer
 
These two lines doesn't seem right
JavaScript
d:"[{"ID":"2","Name":"Office Address","Desc":"Office Address","Check":"True","Code":"/^[a-zA-Z0-9\s\:\-]+$","CreatedBy":"felixd","CreatedDateTime":"2/18/2017 8:05:24 PM","UpdatedBy":"","UpdatedDateTime":"","IsActive":"True","ActiveStatus":"Yes","FlagStatus":"Yes"}]"
console.log(response.d[k].ID);

If you notice that, d is a string, and you can't access the string using key value.

On the other hand if d is an array, then the code will work
JavaScript
var d =[{"ID":"2","Name":"Office Address","Desc":"Office Address","Check":"True","Code":"/^[a-zA-Z0-9\s\:\-]+$","CreatedBy":"felixd","CreatedDateTime":"2/18/2017 8:05:24 PM","UpdatedBy":"","UpdatedDateTime":"","IsActive":"True","ActiveStatus":"Yes","FlagStatus":"Yes"}];
console.log(d[0].ID);

Just like Peter pointed out, the code need JSON.parse. See below on how to access the value after the parse
JavaScript
var data = JSON.parse(d);
$.each(data, function(i, item) {
    x +='ID: ' + item["ID"] + '<br/>Name: ' + item["Name"];
    console.log(item["ID"]);
});

Here is an example.
https://jsfiddle.net/0hnkznqq/2/

basically the d in this example = response.d
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900