9
I have a code similar to this on Jsfiddle. On my home WAMP server I did to try working with JSON (Jsfiddle does not contain the JSON file to test).
In a question I asked about how to insert data into a database with jQuery I was told that it would be a good option to store the data in JSON files. The author of the reply showed me what the syntax of the JSON file would look like. He said he could do the following:
[
{ titulo: 'ET', ano: 1982 },
{ titulo: 'Indiana Jones', ano: 1981 }
]
In my tests with this jsfiddle code worked well when the JSON file was in format:
{
"Titulo": "Até que a sorte nos separe",
"segundo": "segundo valor",
"terceiro": "terceiro valor, o ultimo valor não precisa de virgula"
}
But when I put it in the format that the author of the answer to my first question showed, the click on the link "Click me to list" does not trigger anything.
I would like to know the difference between the two syntaxes, because I want to make a small website with a small database with information about the movies I’ve already watched and the ones I’m still going to see. A link on where to get this information on how to build a JSON file and how to access these values would be a good one.
I use the method $.ajax()
to rescue my JSON that is inside a file *.json by code:
$.ajax({
url: 'js/vendor/testedb.json',
dataType: 'json',
success: function(data) {
var item = [];
$.each(data, function(key,val) {
item.push('<li id="' + key + '">' + val + '</li>');
});
$('<ul/>',{
'class': 'myclass',
html: item.join('')
}).appendTo('body');
},
statusCode: {
404: function() {
alert("some problem");
}
},
});
Clarifying your question about posting JSON here, put your question JSON, if you need help editing, we do it for you. Put as much as you can into the question unless it is something very big (which would probably be wrong to help the problem).
– Maniero
I edited my answer, like you asked, @Pedro Gelli
– Paulo Roberto Rosa
I added a new solution adapted to your @Pedrogelli method, but I advise you to add the code of
$.ajax()
here in the question as it is extremely important.– Paulo Roberto Rosa