I need to make HTML fill itself with content from JSON file using the mustache, or handlebars.
I created two simple HTML templates for testing (using handlebars) and filled them with content from an external Javascript file. http://codepen.io/MaxVelichkin/pen/qNgxpB
Now I need content to lay initially in a JSON file.
I ran into two problems, but they both lie at the heart of solutions of the same main problem - creating a link between the content in the JSON file and HTML, so I decided to ask them in the same question.
(If you think that it is necessary to ask these questions separately, please answer one of the questions, I will remove the second and ask it separately.)
- How can I connect JSON and HTML? As far as I know there is a way, using AJAX, and there's a way that uses a server. AJAX is a new language for me, so I would be grateful for an explanation of how can I do it, using local HTTP server, that I created using Node.JS.
- What should be the syntax in a JSON file? The script in the JSON file must be the same, as a script in Javascript file, but then it should be processed with the help of
JSON.parse
function, is that correct? Or syntax in JSON file should be different?
For example, if we consider my example (link above), the code for the first template in the JSON file must be the same as in the Javascript file, but before the last linedocument.getElementById('quoteData').innerHTML += quoteData;
, I have to write the following linevar contentJS = JSON.parse(quoteData);
, and then change the name of the variable in the last line, so it will be:document.getElementById('quoteData').innerHTML += contentJS;
, Is it right?
Try this:
HTML:
JS:
JSON:
JSON data format derives from JavaScript, so its more look like JavaScript objects, Douglas Crockford originally specified the JSON format, check here.
JavaScript Object Notation has set of rules.
Starts with open curly braces ( { ) and ends with enclosing curly braces ( } ) ex: {}
Inside baces you can add 'key' and its 'value' like { "title" : "hello json"} here "title" is key and "hello json" is value of that key.
"key" should be string
"value" can be:
number
string
Boolean
array
object
Can not add JavaScript comments inside JSON (like // or /**/)
there are many online JSON validators, you can check whether your JSON is valid or not, check here.
When comes to linking JSON to js file, its more like provide an interface to get JSON data and use it in your JavaScript.
here XMLHttpRequest our interface. we usually call XMLHttpRequest API.
In the given js code, to get JSON from the server using an REST API (http://date.jsontest.com/)
for more information on REST API you can check here
from the url: http://date.jsontest.com/ you can get JSON object like below.
So by using external API you can get JSON, to use it in your JavaScript file/ code base you need to convert JSON to JavaScript object,
JSON.parse( /* your JSON object is here */ ) converts JSON to js Object
by using dot(.) or bracket ([]) notation you can access JavaScript Object properties or keys; like below.
So to link local JSON file (from your local directory) or an external API in your JavaScript file you can use "XMLHttpRequest".
'sendGet' function updatedin the above js block with comments please check.
In simple way:
ex: var xhr = new XMLHttpRequest();
ex: xhr.open('GET', "http://date.jsontest.com/");
ex: xhr.send();
ex: xhr.onload = function () {
for more info check here
Know about these:
Existing references:
Basically, JSON is a structured format recently uses which would be prefered due to some advantages via developers, Like simpler and easier structure and etc. Ajax is not a language, It's a technique that you can simply send a request to an API service and update your view partially without reloading the entire page. So you need to make a server-client architecture. In this case all your server-side responses would be sent in
Json
format asRESTFULL
Api, Also you can simply use the json response without any conversion or something else like an array object in javascript. You can see some examples here to figure out better: JSON ExampleHope this can help you. Regards.