我正在尝试构建一个使用PHP生成的JSON的应用程序,然后使用JavaScript进行管理。
目前它没有按预期工作,我不确定原因。
代码片段:
$(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);
}
});
});
});
因此,在更改URL的散列部分后,我会检查永久链接是否列在我拥有的JSON块中。 (固定值)。警报说project.permalink
未定义。
PHP变量$ json是使用PHP数组和json_encode()函数创建的。
JSON在这里:
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"}}];
更新的JSON在这里,建议我删除了包装每个对象的附加对象:
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"}]];
答案 0 :(得分:2)
你的JSON实际上有一个额外的级别没有多大意义:每个项目都包含在一个额外的对象中(为了清楚起见,重新缩进JSON):
[
{
"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) ...
我的猜测是你的PHP代码中生成的JSON不正确,需要更改它以在JSON数组的顶层生成每个项目,而不是包含在其他对象中。
答案 1 :(得分:1)
查看您的JSON数据。你有一个对象列表......
[{"hand-made-cards":{key:val,...}},
{"olive-design":{key:val,...}}];
您确实希望{key:val,...}
成为列表projects
的成员。目前,您有一个对象列表,其中每个对象都有一个键:值对。您可以通过执行project.hand-made-cards.permalink
或project.olive-design.permalink
来引用正确的值,但这可能不是您想要做的。
答案 2 :(得分:0)
根据你的例子json:projects [0] [“hand-made-cars”]。定义了永久链接,但当你在项目中执行每个时,你就“循环”在数组上(其中有0)在我的例子中),你可能想要循环遍历项目[0],或者似乎更好地修复json,并且不要在哈希中添加数组,所以它看起来像:
projects = {"hand-made-cars":{...}, "other-cars": {...}}
此外,如果你的$ json是json_encode的输出,那么看一下http://php.net/manual/en/function.json-encode.php,你就会看到第二个参数。您可能想要使用JSON_FORCE_OBJECT。