F.Kraemer F.Kraemer - 26 days ago 7
HTML Question

Building a blog with JSON and JQuery

This should not be that hard but for some reason my brain wont put it together so now I ask you experts instead.

So I have this project to build a jquery based blog with JSON/HTML/CSS/LESS & Javascript.

My problem is that every post should come from a JSON file and I have no clue how to get it in to the actual Blogg.

JSON file:

{
"post1" : {
"title": "My day",
"content" : "blalbvblblblblblblallksdlmalmdksdkasd",
"date" : "12/0-16",
"author" : "robert",
"image" : "../blogg/img.jpg"
},
"post2" : {
"title": "This right here",
"content" : "blalbvblblblblblblallksdlmalmdksdkasd",
"date" : "12/0-16",
"author" : "robert",
"image" : "../blogg/img.jpg"
},
"post4" : {
"title": "Min vackra",
"content" : "blalbvblblblblblblallksdlmalmdksdkasd",
"date" : "12/0-16",
"author" : "klara",
"image" : "../blogg/img.jpg"
}
}


I have tried using the
$getJSON
method but no progress.

So basically what i need is to get the stuff from the JSON file in to a
div
in my index.html.

I have searched and read for hours and my brain is starting to give up.

So if anyone has an idea or something I should think about, please share!

I will continue to read and try out and will uptade if i find something

Answer
$.ajax({
   dataType: "json",
   url: "myJSONFile.json",
}).done(function(resp){
   for(var p in resp){
     if(!resp.hasOwnProperty(p)) continue;
     var title = resp[p].title;
     var content = resp[p].content;
     // etc...
   }
});

use jquery's $.ajax method