【问题标题】:Using JSON data that does not have "x/y" as its key使用没有“x/y”作为键的 JSON 数据
【发布时间】:2013-05-11 11:03:58
【问题描述】:

我是 JSON 和图形世界的新手,第一次使用 Rickshaw。

在示例中,它使用 JSON 以 x 和 y 作为键的数据:

{ x: 1910, y: 92228531 }

我的数据有以下键值:

{"sampletime":"2013-05-11 11:47:54","samplevalue":"4.39"}

是否可以告诉 Rickshaw 以该格式使用我的数据,或者可以说有办法即时转换此 JSON 数据?

更新:

将 SQL 更改为:

select sampletime as "x", samplevalue AS "y" from rn_qos_data_0011 order by sampletime desc limit 10;

【问题讨论】:

    标签: javascript json rickshaw


    【解决方案1】:

    将 SQL/PHP 更改为:

    $sql = mysql_query("select sampletime as \"x\", samplevalue AS \"y\" from rn_qos_data_0011 order by sampletime desc limit 10;");
    
    $results = array();
    while($row = mysql_fetch_array($sql))
    {
       $results[] = array(
          'x' => strtotime($row['x']),
          'y' => (float)$row['y']
       );
    }
    header('Content-Type: application/json');
    echo json_encode($results);
    

    注意转换为纪元时间和浮点数是必需的。

    【讨论】:

      猜你喜欢
      • 2020-12-23
      • 2014-01-30
      • 1970-01-01
      • 2020-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多