If I have a JSON
file named names.json with:
{"employees":[
{"firstName":"Anna","lastName":"Meyers"},
{"firstName":"Betty","lastName":"Layers"},
{"firstName":"Carl","lastName":"Louis"},
]}
How can I use its content in javascript?
If I have a
How can I use its content in javascript? |
|||||
|
An example how to do this could be:
|
||||
|
Your JSON file does not contain valid JSON. Try the following instead.
You should then see a response. Check out http://jsonlint.com/ |
|||||||||
|
In the jQuery code, you should have the
So it would be like this.
Of course you'll need that property for the non jQuery version too, but you'd need to parse the JSON response first. Also keep in mind that If you're still having trouble, try the full
|
|||||||||
|
You can simply include a Javascript file in your HTML that declares your JSON object as a variable. Then you can access your JSON data from your global Javascript scope using index.html:
data.js:
|
|||||
|
If you want to use PHP.
Optionally, use it async:
The PHP:
|
|||||||||||||||||||||
|