Page 1 of 1

JSON Database

Posted: Thu Jun 21, 2012 12:45 am
by Ark
Hi guys, I wanted to know if it´s possible to save data like: (order: int, player: string, score: int) on a JSON file and also to retrieve that information later on. I know little or nothing about JSON, is it possible to retrieve data with javascript? or do I have to use AJAX?

Thanks for reading!

Re: JSON Database

Posted: Thu Jun 21, 2012 3:33 am
by Jackolantern
MongoDB is essentially a JSON database, although there is a bit more to it than that. It cuts out a lot of the complexity to building, maintaining and querying a relational database such as MySQL.

As far as using JSON data in your applications, you can most definitely get the data with Javascript. That is the easiest language to work with JSON, since JSON is a subset of JS. You can use these methods to work with it:

To turn a JS object into a JSON string:

Code: Select all

var jsonString = JSON.stringify(jsObj);
And to turn it back into a Javascript object from the JSON string:

Code: Select all

var jsObject = JSON.parse(jsonString);
You can do that with AJAX, just in your own scripts, or wherever.

Re: JSON Database

Posted: Fri Jun 22, 2012 3:47 pm
by Ark
Thanks alot jack!

Re: JSON Database

Posted: Fri Jun 22, 2012 10:13 pm
by Jackolantern
Oh, I forgot to mention that the JSON global object used to deal with JSON is part of ECMAscript 5, and thus, is not available in older browsers. For compatibility, Douglas Crockford created a script that adds a JSON global object in browsers that don't already have it. Include the json2.js script on your page, then you can use the global JSON object anywhere.