简体   繁体   English

如何将JSON数据隐藏到数组中以绘制图表?

[英]How to covert JSON data into an array to plot a chart?

I've a php query which creates the JSON data of the the data received from the SQL query that is run. 我有一个php查询,它将创建从运行的SQL查询接收到的数据的JSON数据。 The JSON data is created using this query using this query 使用此查询使用此查询创建JSON数据

echo json_encode($res->fetch_all(MYSQLI_ASSOC), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT );

Now I want to plot a chart using fusionchart for which I need to convert this data into an array. 现在,我想使用Fusionchart绘制图表,为此我需要将此数据转换为数组。 I tried a sample JS code to convert the JSON data into an JS array and it worked 我尝试了一个示例JS代码将JSON数据转换为JS数组,并且它可以正常工作

var data = { "timestamp": "2016-09-23", "value1": "0", "value2": "0", ........., "value49": "0", "value50": "0" };
   var arr = [];
   for (elem in data) {
        arr.push(data[elem]);
    }
    console.log(arr);

Now in the data variable I need to pass the data from php code. 现在,在数据变量中,我需要从php代码传递数据。 This is just one of the records that I entered manually. 这只是我手动输入的记录之一。 There are over a million records and I need to convert all of them. 有超过一百万条记录,我需要将它们全部转换。 How I do this? 我该怎么做?

You can just use Object.values() function to acquire values from an object. 您可以只使用Object.values()函数从对象获取值。

 var data = { "timestamp": "2016-09-23", "value1": "0", "value2": "0", "value49": "0", "value50": "0" }; var arr = Object.values(data); console.log(arr); 

For PHP 对于PHP

You can send only values and omit keys in PHP using following code. 您可以使用以下代码在PHP仅发送值并省略键。 json_decode converts JSON string to an array. json_decodeJSON字符串转换为数组。 implode converts an array to a string. implode将数组转换为字符串。

<?php

$str = '{ "timestamp": "2016-09-23", "value1": "val1", 
        "value2": "val2", "value49": "val49", "value50": "val50" }';
$myJSON = json_decode($str, true);

echo '[' . implode(",", $myJSON) . ']';

Results: 结果:

[2016-09-23,val1,val2,val49,val50] [2016年9月23日,VAL1,val2的,val49,val50]

You can use parseJSON() function : 您可以使用parseJSON()函数:

var obj = jQuery.parseJSON( '{ "name": "John" }' );
alert( obj.name === "John" );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM