解析json通过getJSON提供parseerror

时间:2015-09-12 13:10:26

标签: php jquery arrays json

尝试实现一些jquery.gantt,它需要一些数据。所以手册说json数据。(http://taitems.github.io/jQuery.Gantt/

创建了jquery:

    $(".gantt").gantt({
        source: basePath + "system/print_gantt_project_data.php?project_id=" + projectID,
        navigate: "scroll",
        scale: "weeks",
        maxScale: "months",
        minScale: "days",
        itemsPerPage: 10,
        onItemClick: function(data) {
            alert("Item clicked - show some details");
        },
        onAddClick: function(dt, rowId) {
            alert("Empty space clicked - add an item!");
        },
        onRender: function() {
         console.log("chart rendered");
        }
    });

和一个可以传递json数据的脚本:

$aryOutput = array();
if($project_tasks) {
    foreach($project_tasks as $aryTask) {
        $aryOutput[] =  array(
            'name' => $aryTask['pt_name'],
            'desc' => $aryTask['pt_name'],
            'values' => array(array(
            'to' => '/Date('.strtotime($aryTask['pt_end_date']).')/',
            'from' => '/Date('.strtotime($aryTask['pt_start_date']).')/',
            'desc' => $aryTask['pt_description'],
            'label' => $aryTask['pt_description']
        ))
        );
    }
}
$strJSON = json_encode($aryOutput);
header('Content-type:application/json;charset=utf-8');
echo $strJSON;

在调查json时:

    [{"name":"test3","desc":"test3","values":[{"to":"\/Date(1442268000)\/","from":"\/Date(1442181600)\/","desc":"test3","label":"test3"}]},{"name":"test1","desc":"test1","values":[{"to":"\/Date(1442268000)\/","from":"\/Date(1442095200)\/","desc":"test1","label":"test1"}]},{"name":"test2","desc":"test2","values":[{"to":"\/Date(1442268000)\/","from":"\/Date(1442268000)\/","desc":"test2","label":"test2"}]}]

取自chrome中的console.log。 (基于插入jquery.fn.gantt.js的代码:

            // **Create the chart**
            create: function (element) {

                // Initialize data with a json object or fetch via an xhr
                // request depending on `settings.source`
                if (typeof settings.source !== "string") {
                    element.data = settings.source;
                    core.init(element);
                } else {
                    $.getJSON(settings.source, function (jsData) {
                        element.data = jsData;
                        core.init(element);

                    })
  .fail(function(jqXHR, textStatus, errorThrown) { console.log('getJSON request failed! ' + textStatus); })
                }
            },

" getJSON请求失败! parsererror"

问题是...... json似乎是合法的 - 但是解析错误似乎与此相矛盾。

任何人?

1 个答案:

答案 0 :(得分:0)

Aaaand所以我错了。看着控制台(shift + ctrl + i)chrome我找到了罪魁祸首。这是一个UTF8-BOM。因此,将我的脚本文件转换为ASCII / DOS就完全可以了。

不知道为什么每个人都说UTF8是“狗屎”;)