Reputation: 8663
I'm attempting to build an application that uses JSON generated using PHP which is then managed using JavaScript.
At the moment it is not working as expected and I'm not sure why.
Snippet of code:
$(document).ready(function(){
var projects = <?= $json; ?>;
$(window).bind('hashchange', function(){
var potential = window.location.hash.substring(1);
$.each(projects, function(i,project){
if (project.permalink == potential)
{
alert(project.title);
$('#title').text(project.title);
$('#agency').text(project.agency);
}
else
{
alert('potential: ' + potential + '. project.permalink: ' + project.permalink);
}
});
});
});
So after changing the URL's hash section, I check to see if the permalink is listed in a block of JSON I have. (The permalink value). The alert says that project.permalink
is undefined.
The PHP variable $json is created using PHP arrays and the json_encode() function.
The JSON is here:
var projects = [{"hand-made-cards":{"id":"3","title":"Hand Made Cards","type":"","description":"","website_url":"http:\/\/northumberlhand-made.co.uk","agency":"-","permalink":"hand-made-cards","position":"1","added_by":"1","updated_by":"1","deleted_by":"0","published":"1","deleted":"0","datetime_added":"2012-06-03 16:42:14","datetime_updated":"2012-06-03 17:44:37","datetime_deleted":"0000-00-00 00:00:00","rel_id":"13"}},{"olive-design":{"id":"2","title":"Olive Design","type":"","description":"Olive Design website description. What happened here?","website_url":"http:\/\/olive-design.co.uk","agency":"Gardiner Richardson","permalink":"olive-design","position":"2","added_by":"1","updated_by":"1","deleted_by":"0","published":"1","deleted":"0","datetime_added":"2012-06-03 16:41:31","datetime_updated":"2012-06-03 17:43:50","datetime_deleted":"0000-00-00 00:00:00","rel_id":"14"}},{"riba-microsite":{"id":"1","title":"RIBA Microsite","type":"","description":"Some info abou the RIBA MS.","website_url":"http:\/\/ram.grtest.com","agency":"Gardiner Richardson","permalink":"riba-microsite","position":"3","added_by":"1","updated_by":"1","deleted_by":"0","published":"1","deleted":"0","datetime_added":"2012-06-03 16:40:55","datetime_updated":"2012-06-03 17:43:29","datetime_deleted":"0000-00-00 00:00:00","rel_id":"15"}}];
Updated JSON is here, as advised I have removed the additional object wrapping each object:
var projects = [[{"id":"3","title":"Hand Made Cards","type":"","description":"","website_url":"http:\/\/northumberlhand-made.co.uk","agency":"-","permalink":"hand-made-cards","position":"1","added_by":"1","updated_by":"1","deleted_by":"0","published":"1","deleted":"0","datetime_added":"2012-06-03 16:42:14","datetime_updated":"2012-06-03 17:44:37","datetime_deleted":"0000-00-00 00:00:00","rel_id":"13"}],[{"id":"2","title":"Olive Design","type":"","description":"Olive Design website description. What happened here?","website_url":"http:\/\/olive-design.co.uk","agency":"Gardiner Richardson","permalink":"olive-design","position":"2","added_by":"1","updated_by":"1","deleted_by":"0","published":"1","deleted":"0","datetime_added":"2012-06-03 16:41:31","datetime_updated":"2012-06-03 17:43:50","datetime_deleted":"0000-00-00 00:00:00","rel_id":"14"}],[{"id":"1","title":"RIBA Microsite","type":"","description":"Some info abou the RIBA MS.","website_url":"http:\/\/ram.grtest.com","agency":"Gardiner Richardson","permalink":"riba-microsite","position":"3","added_by":"1","updated_by":"1","deleted_by":"0","published":"1","deleted":"0","datetime_added":"2012-06-03 16:40:55","datetime_updated":"2012-06-03 17:43:29","datetime_deleted":"0000-00-00 00:00:00","rel_id":"15"}]];
Upvotes: 2
Views: 99
Reputation: 19237
according to your example json: projects[0]["hand-made-cars"].permalink is defined, but when you do the each on projects, then you "loop" over the array (where there is the 0 in my example), and you probably want to loop over projects[0], or what seems to be better to fix the json, and don't add the array arount the hash, so it'll look like:
projects = {"hand-made-cars":{...}, "other-cars": {...}}
Also if your $json is the output of json_encode, then have a look at http://php.net/manual/en/function.json-encode.php and you'll see that there's a 2nd parameter. You might want to use JSON_FORCE_OBJECT.
Upvotes: 0
Reputation: 6275
Take a look at your JSON data. You have a list of objects...
[{"hand-made-cards":{key:val,...}},
{"olive-design":{key:val,...}}];
You really want the {key:val,...}
as the members of the list projects
. Currently you have a list of objects where each object has one key : value pair. You could reference the correct values by doing project.hand-made-cards.permalink
or project.olive-design.permalink
but that's probably not what you want to do.
Upvotes: 1
Reputation: 43168
You actually have an extra level in your JSON that does not make much sense: each project is enclosed in an additional object (re-indented the JSON for clarity):
[
{
"hand-made-cards": {
"id":"3",
"title":"Hand Made Cards",
"type":"",
"description":"",
"website_url":"http:\/\/northumberlhand-made.co.uk",
"agency":"-",
"permalink":"hand-made-cards",
"position":"1",
"added_by":"1",
"updated_by":"1",
"deleted_by":"0",
"published":"1",
"deleted":"0",
"datetime_added":"2012-06-03 16:42:14",
"datetime_updated":"2012-06-03 17:44:37",
"datetime_deleted":"0000-00-00 00:00:00",
"rel_id":"13"
}
},
{
"olive-design": {
"id":"2",
"title":"Olive Design",
"type":"",
"description":"Olive Design website description. What happened here?",
"website_url":"http:\/\/olive-design.co.uk",
"agency":"Gardiner Richardson",
"permalink":"olive-design",
"position":"2",
"added_by":"1",
"updated_by":"1",
"deleted_by":"0",
"published":"1",
"deleted":"0",
"datetime_added":"2012-06-03 16:41:31",
"datetime_updated":"2012-06-03 17:43:50",
"datetime_deleted":"0000-00-00 00:00:00",
"rel_id":"14"
}
},
... (snip) ...
My guess is you've got the JSON generated incorrectly in your PHP code, and need to change it to generate each project on the top-level in the JSON array instead of wrapped in an additional object.
Upvotes: 2