-1

I am trying to access an array of objects, with javascript, that is stored in a json file on my site. I could just include the array of objects in my js, but I'm trying to learn how to use json files.

I would like to store the array as a variable in my js file. What is the best way to go about this?

Jake 1986
  • 535
  • 3
  • 24

1 Answers1

-1

You just need to pass the String to:

JSON.parse("{my:'object'}")

To read the file from the server, just use the ajax api.

$.get( "my-endpoint/test.json", function( data ) {
  var jsObject = JSON.parse(data)
});

I guess Jquery might already have parsed the data and you do not even need to call JSON.parse() - try it out :)

Tarion
  • 15,045
  • 11
  • 64
  • 105