Could someone please help to figure out how can I group and display JSON objects? Here's my example: For example, I have a JSON file called "results.php":
[
{ "name": "project1", "url": "picture1-1.jpg"},
{ "name": "project1", "url": "picture1-2.jpg"},
{ "name": "project2", "url": "picture2-1.jpg"},
{ "name": "project3", "url": "picture3-1.jpg"},
{ "name": "project1", "url": "picture1-3.jpg"},
{ "name": "project4", "url": "picture4-1.jpg"},
{ "name": "project3", "url": "picture3-2.jpg"},
{ "name": "project1", "url": "picture1-4.jpg"}
]
And I want to display all of these projects in separate divs, i.e. project 1 should contain only pictures picture1-1.jpg, picture1-2.jpg, picture1-3.jpg and picture1-4.jpg and so on for every project. I tried to use this code to display JSON data in a loop, and it works fine:
var source="results.php";
$.getJSON(source,function(json){
$.each(json, function(i,post){
if (post.name)
$(".body-wrapper").append('<div class="post"><h1>'+post.name+'</h1><p>'+post.url+'</p></div>');
});
});
But I still can't figure out how to display JSON data separately for every project in JSON file.
Hope you can help me to solve this issue. I would be very thankful for this.
Thank you in advance! :)
Best regards, Alex