dtf1111 2013-06-02 18:17
浏览 110
已采纳

以json对象的形式传递时间戳值以填充flot图表

I am trying to pass the values at different time-stamps by quering from MongoDb, then passing them to client in form of JSON.

Then client will read the JSON object returned by php script, and on the basis of that will populate the flot chart.

I am not being able to do this, and have the following two doubts/questions:

A. In the JSON object that is being returned by php, what should be the way of constructing JSON object ?

At present, from the following code, the JSON is being produced like:

[{"1371859200000":65},{"1371860100000":63},{"1371861000000":48},{"1371861900000":47},{"1371862800000":41},{"1371863700000":19},{"1371864600000":35}]

Is the above right ? OR should the data be something like : [["1371859200000":65],["1371860100000":63], ....]

What is the difference between the two of them ? (In the above '[' is used instead of curly braces)

B. In PHP, strtotime returns int. BUT, when i echo my JSON object, the timestamp is produced like "1371859200000" (i.e. a string) and not 1371859200000 (i.e. an int). Why so ? I have to populate the flot chart, and it is necessary for the timestamp to be present as int (and not as string) in order to populate the flot chart.

Following is my PHP code (not the whole code):

$date = 2;
$tsc = 15;
$result = array();
for ($hour = 0; $hour  < 24; $hour++) {
    for ($min_lower = 0; $min_lower <= 60-$tsc; $min_lower += $tsc) {
        // Query MongoDB.
        $query = array( "date" => $date, "hour" => $hour, "minutes" => array( '$gte' => $min_lower, "\$lt" => $min_lower+$tsc ) );
        $count = $coll->find($query)->count();
        $time = strtotime("2013-06-".strval($date)." ".strval($hour).":".strval($min_lower).":".strval(0)." UTC") * 1000;
        $data = array($time => $count);
        // Push this smaller array into the main array. Is this the correct way ?
        array_push($result, $data);
    }
}
echo json_encode($result);

And following is my javascript/jquery code:

var options = {
    lines: {
        show: true
    },
    points: {
        show: true
    },
    xaxis: {
        mode : "time",
        timeformat: "%m/%d/%y"
     }
};

$("button.fetch").click(function () {
    var button = $(this);
    var dataurl = "MY URL";// Here the URL comes.

    function onDataReceived(series) {
        data.push(series);
        $.plot("#placeholder", series, options);
    }
    $.ajax({
        url: dataurl,
        type: "GET",
        dataType: "json",
        success: onDataReceived
    });
 })
  • 写回答

1条回答 默认 最新

  • doujiunai2169 2013-06-02 18:24
    关注

    A. [[key: value]] is not valid JavaScript or JSON syntax. You can only have key value pairs with objects, i.e. the curly brace syntax.

    B. In the same vein, object keys can only be strings, not numbers. It is not valid JSON syntax to have {123: "value"}, it would have to be {"123": "value"}.

    Two possible solutions would be to

    1. Simply convert the keys to numbers on the JS side; you can probably depend on the fact that they will always be numbers
    2. Represent the data differently, like [{"ts": 13456000, "count": 65}] since values can be numbers even though keys cannot.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀