2

我正在尝试构建一个应用程序,该应用程序使用使用 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"}]];
4

3 回答 3

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 数组的顶层生成每个项目,而不是包装在附加对象中。

于 2012-06-03T20:20:27.680 回答
1

查看您的 JSON 数据。你有一个对象列表...

[{"hand-made-cards":{key:val,...}}, 
 {"olive-design":{key:val,...}}];

你真的希望{key:val,...}作为列表的成员projects。目前,您有一个对象列表,其中每个对象都有一个键:值对。project.hand-made-cards.permalink您可以通过做或引用正确的值,project.olive-design.permalink但这可能不是您想要做的。

于 2012-06-03T20:22:10.440 回答
0

根据您的示例 json: projects[0]["hand-made-cars"].permalink 已定义,但是当您在项目上执行 each 时,您会“循环”数组(在我的示例中有 0 ),并且您可能想要遍历项目 [0],或者似乎更好地修复 json,并且不要在哈希周围添加数组,所以它看起来像:

projects = {"hand-made-cars":{...}, "other-cars": {...}}

此外,如果你的 $json 是 json_encode 的输出,那么看看http://php.net/manual/en/function.json-encode.php你会看到有第二个参数。您可能想要使用 JSON_FORCE_OBJECT。

于 2012-06-03T20:18:13.967 回答