如何将带换行符的逗号分隔字符串解析为 PHP 中的 JSON 以与 Morris.JS 一起使用
How to parse Comma delimited string with newline to JSON in PHP to use with 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");
$json2 = json_encode($Data);
未正确解析以与 MORRIS.JS 一起使用,结果完全是垃圾
"users"、"actions"、"movements"就是headers。
有人能给我指出正确的方向吗?
所需的结果应如下所示:
[0] => Array
(
[user] => user1
[actions] => 630
[movements] => 87
)
[1] => Array
(
[user] => user2
[actions] => 330
[movements] => 187
)
等等
您需要先将字符串转换为数组,然后再使用 JSON, try to use explode 进行编码:
$result = explode(',', $string);
$json = json_encode($result);
如果您不想忽略新行:
$result = explode(' ', $string);
$i = 0;
foreach ($result as $item){
$resultJ[$i] = explode(',', $item);
$i++;
}
$json = json_encode($resultJ);
删除 json 中的斜杠和 \n:
您可以在 json:
中使用 str_replace
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
?>
我向服务发送 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");
$json2 = json_encode($Data);
未正确解析以与 MORRIS.JS 一起使用,结果完全是垃圾
"users"、"actions"、"movements"就是headers。 有人能给我指出正确的方向吗?
所需的结果应如下所示:
[0] => Array
(
[user] => user1
[actions] => 630
[movements] => 87
)
[1] => Array
(
[user] => user2
[actions] => 330
[movements] => 187
)
等等
您需要先将字符串转换为数组,然后再使用 JSON, try to use explode 进行编码:
$result = explode(',', $string);
$json = json_encode($result);
如果您不想忽略新行:
$result = explode(' ', $string);
$i = 0;
foreach ($result as $item){
$resultJ[$i] = explode(',', $item);
$i++;
}
$json = json_encode($resultJ);
删除 json 中的斜杠和 \n: 您可以在 json:
中使用 str_replacestr_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
?>