我将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无法正确解析并导致完全垃圾
“用户”,“动作”,“动作”是标题。 有人能指出我正确的方向吗?
所需的结果应如下所示:
[0] => Array
(
[user] => user1
[actions] => 630
[movements] => 87
)
[1] => Array
(
[user] => user2
[actions] => 330
[movements] => 187
)
等等
答案 0 :(得分:1)
你总是把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"}
]
*/
答案 1 :(得分:0)
您需要在使用JSON进行编码之前将字符串转换为数组,请尝试使用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&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);
答案 2 :(得分:0)
我终于以这种方式实现了这个目标:
<?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
?>