0

i get this JSON from my REST:

{"users":[{"id_user":"1","firstname":"Admin","lastname":"Admin","mail":"[email protected]","password":"admin"}]} 

and with follow code i want to parse the JSON response.

$.getJSON("http://localhost/REST/users", function(data) { var jsonData = JSON.parse(data); for (var i = 0; i < jsonData.counters.length; i++) { var counter = jsonData.counters[i]; console.log(counter.counter_name); } }); 

but in the console i get this error message:

SyntaxError: JSON.parse: unexpected character at line 1 column 2 of the JSON data 

I hope you can help me =/

1
  • also add dataType: "json" Commented Jul 5, 2014 at 8:47

1 Answer 1

1

Since you're using getJSON you don't need to convert your response to JSON manually. jQuery does that for you.

$.getJSON("http://localhost/REST/users", function(data) { for (var i = 0; i < data.counters.length; i++) { var counter = data.counters[i]; console.log(counter.counter_name); } }); 

Although keep in mind, that the JSON response you posted doesn't have a counters key, so I'm not entirely sure what you want to achieve with this.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.