使用php将JSON字符串重新格式化为morris图表格式 [英] Reformat JSON string with php to morris charts formats

查看:108
本文介绍了使用php将JSON字符串重新格式化为morris图表格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将远程csv文件转换为JSON后,我具有以下json

I have the following json after converting a remote csv file to JSON

将csv的代码转换为json

convert code for csv to json

/////////////////////////////////////////////////////////
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, '');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Authorization: Token ****',
    'Accept-Version: ***',
));
$data = curl_exec($ch);
///////////////////////////////////////////////////////////////



if (curl_errno($ch)) {
print "Error: " . curl_error($ch);
} else {
// Show me the result
//var_dump($data);
curl_close($ch);
$array = array_map("str_getcsv", explode("\n", $data));
$encode = json_encode($array, true);

///////////////////////////////////////

它产生了这个:

[
"Timestamp",
"Point",
"Value",
"2016-03-11T14:40:00+00:00",
"ategho-leg_1-8",
"487.0",
"2016-03-11T14:40:00+00:00",
"ategho-leg_2-8",
"488.0",
"2016-03-11T14:40:00+00:00",
"ategho-leg_3-8",
"484.0",
"2016-03-11T14:40:00+00:00",
"ategho-temperature_long-8",
"466.0",
"2016-03-11T14:40:00+00:00",
"ategho-temperature_short-8",
"198.0",
"2016-03-11T14:45:00+00:00",
"ategho-leg_1-8",
"482.0",
"2016-03-11T14:45:00+00:00",
"ategho-leg_2-8",
"490.0",
"2016-03-11T14:45:00+00:00",
"ategho-leg_3-8",
"479.0",
"2016-03-11T14:45:00+00:00",
"ategho-temperature_long-8",
"464.0",
"2016-03-11T14:45:00+00:00",
"ategho-temperature_short-8",
"199.0",
null

]

我想将其转换为用于莫里斯图表的数据,该图表需要采用以下格式的数据:

And I want to turn it into data for use in morris charts, which wants the data in this format:

[
{"Timestamp":"2016-03-11T14:40:00+00:00", "Point":"ategho-leg_1-8", "Value":"487.0"}
]

我已经搜索过堆栈并尝试了很多事情,但是我无法切换格式.我也尝试过这样的事情:

I have searched stack and tried numersous things but I cannot get it to switch formats . I also tried stuff like this:

$new_final = array();
foreach($array as $value) {
    foreach($value as $sub_value) {
        $new_final[] = $sub_value;
    }
}

echo json_encode($new_final);

感谢您的帮助.谢谢

EDIt:第一个答案给了我这样的代码:

EDIt: the first answer gives me code like this :

{
  "Timestamp":[
     "2016-03-11T14:40:00+00:00",
     "ategho-leg_3-8",
     "484.0"
  ],
  "Point":[
     "2016-03-11T14:40:00+00:00",
     "ategho-temperature_long-8",
     "466.0"
  ],
  "Value":[
     "2016-03-11T14:40:00+00:00",
     "ategho-temperature_short-8",
     "198.0"
  ]
  },

推荐答案

最好的方法是直接处理csv中的数据,而无需在JSON中进行预编码. 顺便说一句,这将起作用:

The best way is to process directly data from csv without pre-encoding in JSON. BTW, this will works:

$data = json_decode( $json );

$result = array();
for( $i = 3; $i< count( $data ); $i=$i+3 )
{
    $result[] = array
    (
        'Timestamp' => $data[$i],
        'Point'     => $data[$i+1],
        'Value'     => $data[$i+2]
    );
}

echo json_encode( $result );

eval.in演示

基本上,您需要一个从第3个项目开始并重复每3个项目的for()`循环,然后可以将与您的 morris图表格式的关联数组添加到主数组中.

Basically, you need a for()` loop that start from 3rd item and that repeat each 3 items, then you can add to main array an associative array with your morris chart format.

在您的示例中,最后一个JSON项为Null,因此在转换为JSON之前,您必须确定是unset()还是unset() $result的最后一个元素.

In your example last JSON item is Null, so you have to decide if unset() it or if unset() last element of $result before converting to JSON.

在修改了问题之后,您可以通过以下方式处理数组:

With modified question, you can process your array in this way:

$result = array();
for( $i = 1; $i < count( $array ); $i++ )
{
    $result[] = array
    (
        'Timestamp' => $array[$i][0],
        'Point'     => $array[$i][1],
        'Value'     => $array[$i][2]
    );
}
echo json_encode( $result );

之前,如果Null问题仍然存在.

Check before if the Null issue persists.

请注意for()循环从1开始,以避免包含标头.如果没有头,则必须从0开始循环.

Please note that for() loop start from 1 to avoid header inclusion. If you doesn't have heads, you have to start loop form 0.

这篇关于使用php将JSON字符串重新格式化为morris图表格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆