如何在PHP中将带有换行符的逗号分隔字符串解析为JSON,以便与Morris.JS一起使用
我向服务发送SOAP API请求,但响应出现在一个字段中,由字符串组成:如何在PHP中将带有换行符的逗号分隔字符串解析为JSON,以便与Morris.JS一起使用,php,json,parsing,morris.js,Php,Json,Parsing,Morris.js,我向服务发送SOAP API请求,但响应出现在一个字段中,由字符串组成: $string = '"users","actions","movements" "user1","111","54" "user2","87","123" "user3","92","23"' 当我这样做时: $json = json_encode($string); 不会产生有效的JSON 我试过: $Data = str_getcsv($incomingdata, "," , '"' , "\n"); $jso
$string = '"users","actions","movements" "user1","111","54" "user2","87","123" "user3","92","23"'
当我这样做时:
$json = json_encode($string);
不会产生有效的JSON
我试过:
$Data = str_getcsv($incomingdata, "," , '"' , "\n");
$json2 = json_encode($Data);
它没有正确解析以与MORRIS.JS一起使用,结果是完全的垃圾
“用户”、“动作”、“移动”是标题。
有人能给我指一下正确的方向吗
所需结果应如下所示:
[0] => Array
(
[user] => user1
[actions] => 630
[movements] => 87
)
[1] => Array
(
[user] => user2
[actions] => 330
[movements] => 187
)
依此类推在编码之前,您需要将字符串转换为数组,请尝试改用: 如果不想忽略新行,请执行以下操作:
$result = explode(' ', $string);
$i = 0;
foreach ($result as $item){
$resultJ[$i] = explode(',', $item);
$i++;
}
$json = json_encode($resultJ);
要删除json中的斜杠和\n,请执行以下操作:
您可以在json中使用:
str_replace(array("\\", "\n"), "", $json);
或者在$string中(推荐),因为您的\是通过json编码自然转义到:
str_replace('"', "", $string);
问题更新,答案也更新:
<?php
$string = str_replace('"', "", '"users","actions","movements" "user1","111","54" "user2","87","123" "user3","92","23"');
$result = explode(' ', $string);
$result2 = array();
$i = 0;
//Load the arrays
foreach ($result as $item) {
$result2Array[$i] = explode(',', $item);
$i++;
}
$total = count($result2Array) - 1;
for ($i = 1; $i <= $total; $i++) {
$j = 0;
//Bring values to each index
foreach ($result2Array[0] as $index){
$resultF[$i-1][$index] = $result2Array[$i][$j];
$j++;
}
}
var_dump($resultF);
$json = json_encode($resultF);
您应该使用预期的json示例,无论如何,这可能会帮助您:
$string = '"users","actions","movements"
"user1","111","54"
"user2","87","123"
"user3","92","23"';
// in case of csv where rows are delimited with new lines use explode( "\n", $string )
// if is delimited with space character use explode( ' ', $string )
$array = array_map( 'str_getcsv', explode( "\n", $string ) );
array_shift( $array );
array_walk( $array, function( &$v, $k, $keys ) {
$v = array_combine( $keys, $v );
}, [ 'user', 'actions', 'movements' ] );
print_r( $array );
/*
Array
(
[0] => Array
(
[user] => user1
[actions] => 111
[movements] => 54
)
[1] => Array
(
[user] => user2
[actions] => 87
[movements] => 123
)
...
)
*/
print_r( json_encode( $array ) );
/*
[
{"user":"user1","actions":"111","movements":"54"},
{"user":"user2","actions":"87","movements":"123"},
{"user":"user3","actions":"92","movements":"23"}
]
*/
我终于这样做了:
<?php
include("functions.php");
$data = MYFunction('Template','FilterBy','SortBy','Colums'); //here DATA FROM SOAP
//............
//............
$data = $pickrate;
file_put_contents('file.csv', $pickrate); // placing acquired data to CSV
$csv= file_get_contents('file.csv'); //open saved csv file
$array = array_map("str_getcsv", explode("\n", $csv)); //create array map and explode on \n
if (($handle = fopen('file.csv', 'r')) === false) {
die('Error opening file'); // no access error handling
}
$headers = fgetcsv($handle, 1024, ','); //generate headers from first row delim, by ","
$complete = array(); //opening array
while ($row = fgetcsv($handle, 1024, ',')) { //read each row delim by ","
$complete[] = array_combine($headers, $row); // push it into var $complete and combine $headers row with $row
}
fclose($handle); // closes the array gen
//include this file and then call <? echo json_encode($complete); ?> in Morris.js DATA field
?>
Morris.js数据字段中的
?>
hmm这听起来很合理,除了::array\u combine():两个参数在…($array[$k]=array\u combine($keys,$v);)中的元素数应该相等。对于你的解决方案,除了PHP警告之外,我差得太远了。你能看看我更新过的问题吗。我正在尝试。嗯,如何摆脱那些slaash和\n的?[]\“user\”、“\”actions\“\n\”user1\”、“\”265\“\n\”user2\”、“\”198\“\n”]]您可以在json中使用str\u replace:str\u replace(数组(“\\”、“\n”)、“,$json);或者在$string中(推荐):str_replace(“,”,$json);更新以满足您的实际需要
<?php
include("functions.php");
$data = MYFunction('Template','FilterBy','SortBy','Colums'); //here DATA FROM SOAP
//............
//............
$data = $pickrate;
file_put_contents('file.csv', $pickrate); // placing acquired data to CSV
$csv= file_get_contents('file.csv'); //open saved csv file
$array = array_map("str_getcsv", explode("\n", $csv)); //create array map and explode on \n
if (($handle = fopen('file.csv', 'r')) === false) {
die('Error opening file'); // no access error handling
}
$headers = fgetcsv($handle, 1024, ','); //generate headers from first row delim, by ","
$complete = array(); //opening array
while ($row = fgetcsv($handle, 1024, ',')) { //read each row delim by ","
$complete[] = array_combine($headers, $row); // push it into var $complete and combine $headers row with $row
}
fclose($handle); // closes the array gen
//include this file and then call <? echo json_encode($complete); ?> in Morris.js DATA field
?>