-4

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

6
  • Why do you think data is a string? Commented Nov 6, 2018 at 17:04
  • Becouse you use ' and not " that not a valid JSON format. Commented Nov 6, 2018 at 17:04
  • It is being returned from an mvc5 controller method as a string Commented Nov 6, 2018 at 17:05
  • Would you please edit the question to reflect the real data. Commented Nov 6, 2018 at 17:06
  • 1
    You say in the comment above, that you're getting data as a string, however, data in the question is not a string, it's an array. Also, if your server responses with such a string when requesting JSON, there's something really off on your server ... Commented Nov 6, 2018 at 17:36

2 Answers 2

0

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:

  • Requires strings be quoted with " not '
  • Does not allow trailing , 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).

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

Comments

0

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

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.