我需要从我从mysql数据库获得的数据添加指向。在这个时候,我得到的JSON数据,但我不知道为什么在输出数组中,数据有双引号(”“)是这样的:Highcharts用json添加指向折线图
["{name:'Chip 3',data:[[moment('2015-05-14 13:26:21','HH:mm:ss').valueOf(),29],[moment('2015-05-14 13
:26:51','HH:mm:ss').valueOf(),29],[moment('2015-05-14 13:27:21','HH:mm:ss').valueOf(),29],[moment('2015-05-14
13:27:51','HH:mm:ss').valueOf(),29],[moment('2015-05-14 13:28:21','HH:mm:ss').valueOf(),29],[moment('2015-05-14
14:42:54','HH:mm:ss').valueOf(),32],]}"]
所以Highcharts无法访问数据并显示数据在图表上。现在我需要删除数组中的双引号或者做其他事情来使Highcharts可以对数据进行regconize。
这是我用于获取和更新系列数据的data.php文件中的代码。
<?php
header("Content-type: text/json");
include_once 'include/connection.php';
$db = new DB_Class();
$query = "select distinct idchip from datatable ";
$result = mysql_query($query);
$rows = array();
$count = 0;
$getall = array();
while($row = mysql_fetch_array($result)) {
$table = array();
$query2 = "select datetime,temperature from datatable where idchip=".$row['idchip'].' group by datetime ';
$dataresult = mysql_query($query2);
while($datarow = mysql_fetch_array($dataresult))
{
$data = '';
$datatimes .= $datarow['0'].',';
$data .= "[moment('".$datarow['0']."','HH:mm:ss').valueOf(),".(integer)$datarow['1']."],";
$stringdata .= $data;
}
$newstring = $stringdata ;
$stringdata = '';
$stringtime = '';
$namedata = "{name:'Chip ".$row["idchip"]."',data:[$newstring]}";
$getall[] = $namedata;
}
echo json_encode($getall);
?>
这是我用来获取ajax返回数据的代码。
function getData() {
jQuery.ajax({
url: 'data.php',
type: 'GET',
dataType: 'json',
mimeType: 'multipart/form-data',
contentType: false,
cache: false,
processData: false,
success: function(data, jqXHR) {
if(data == "null") {
} else {
$.getJSON("data.php", function(json) {
chart = new Highcharts.Chart({
chart: {
renderTo: 'container',
defaultSeriesType: 'spline',
events: {
// load: requestData
}
},
series: json
});
});
}
},
error: function(textStatus) {
console.log(" error. damm. ");
//console.log(error);
}
});
}
问题是您构建在PHP中JSON对象的方式。请查看这[json](http://stackoverflow.com/questions/682260/returning-json-from-php-to-javascript)。您不必创建json字符串。你需要创建最终数组,然后只需使用json_encode($ getall);这将做到 –
@irfanrasool问题是如果我通过json数组添加我不能添加[时刻(...)]或这样的数组中的一些事情。你能告诉我怎么做这个请。 –
一般在json中你不能使用函数,因为不会运行/评估,所以irfan rasool的建议是正确的。 –