I am trying to parse a JSON file that has this code:
{
"employees": [
{ "firstName":"John" , "lastName":"Doe" },
{ "firstName":"Anna" , "lastName":"Smith" },
{ "firstName":"Peter" , "lastName":"Jones" }
]
}
with this small script:
<script>
var obj = $.parseJSON("employe.json")
</script>
I just want to use the employees object, but I am getting this error:
SyntaxError: JSON.parse: unexpected character @ http://code.jquery.com/jquery-1.9.1.min.js:3
I also tried the same thing with getJSON, but it did not work either.
Is getJSON for external files and parseJSON for json string?
parseJSONparses json strings into objects.getJSONusesparseJSONinternally, so it does the parsing automagically. When you get the "unexpected character" error, it's almost always because the JSON is invalid, and there are characters that should'nt be there.