So let me save this and I'll switch back to a browser, refresh our page, And if you have the console openm remember that you could just right-click on this page and select Inspect Element if you are using Google Chrome like me. And I can see the series of objects that this returns. If I open that up, I can see each individual object and inside each object, I can see different name and value pairs. So this is a little different from what we would see with XML. It's essentially an array of objects and each object has a label and the data from our file.
So here's the bad news: JSON parse is not available in some older browsers. So, if you go to this website, you could see that it's available in most modern browsers, but a lot of the older versions of IE do not support the parse command. So your options are to use EVAL--but that has some security issues--write your own parser, or use an existing library. jQuery for example, takes care of this very well. Let's go back to our code. So we'll modify our code so that it outputs a list of speakers. We'll start by creating the output variable, just like we did with XML, and then in here we'll loop through the valleys of the objects.
To do that I am going to use the for in statement. Finally, we'll target the update element with the results of our output variable. So let's save this and switch back to our browser, refresh the page, and you can see that the list appears. So working with JSON files is pretty easy with the parse command. However, it might not be available in all browser versions. So for ultimate compatibility, I recommend that you use a library like jQuery.
- What is AJAX?
- Making requests asynchronous
- Updating the DOM with getElementById
- Reading JSON files
- Working with jQuery and AJAX
- Sending and searching JSON data
- Incorporating CSS transitions