I am trying to read the values of properties in this string, when i try to parse it , i get invalid character. Can you tell me whats wrong here
data = [{'title' : 'location 1','lat' : '29.769730','lng' : '-95.257181','desc' : 'Apartments',},{'title' : 'location 2','lat' : '29.852264','lng' : '-95.469999','desc' : 'location description',},];
var test = $.parseJSON(data)l
error - Unhandled exception at line 138, column 13 in http://localhost:17765/Loc/index 0x800a03f6 - JavaScript runtime error: Invalid character
In your code, data
is not a string. It is an array. It does not need parsing (other than by the JavaScript compiler). Just discard $.parseJSON
and work with the data.
data = [{'title' : 'location 1','lat' : '29.769730','lng' : '-95.257181','desc' : 'Apartments',},{'title' : 'location 2','lat' : '29.852264','lng' : '-95.469999','desc' : 'location description',},]; data.forEach(o => console.log(o.title));
It is being returned from an mvc5 controller method as a string
If your code does not accurately reflect the data you have and you do have a string, then it would need parsing.
The code you provided is, however, not valid JSON which:
"
not '
,
after the last item in an array.You need to fix the server side code so it returns real JSON.
This will probably involve replacing some code which attempts to generate JSON by mashing together strings with some which uses a JSON aware library function (see this question ).
Your JSON is invalid, try this:
var data = '[{"title" : "location 1","lat" : "29.769730","lng" : "-95.257181","desc" : "Apartments"},{"title" : "location 2","lat" : "29.852264","lng" : "-95.469999","desc" : "location description"}]';
var test = $.parseJSON(data);
validate your JSON here
[{"title" : "location 1","lat" : "29.769730","lng" : "-95.257181","desc" : "Apartments"},{"title" : "location 2","lat" : "29.852264","lng" : "-95.469999","desc" : "location description"}]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.