2017-05-25 88 views
0

我发送SOAP API请求服务来使用,但响应进来一个领域,由一个字符串:如何解析逗号分隔字符串换行符JSON在PHP与Morris.JS

$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

我终于做到了出现这种情况了:

<?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 
?> 
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); 

要删除的斜线和\ n在您的JSON: 您可以在JSON使用str_replace:由

str_replace(array("\\", "\n"), "", $json); 

或$字符串(推荐),因为你的\是一个自然逃往“ JSON编码:

str_replace('"', "", $string); 

QUESTION更新ANSWER TOO:

<?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); 
+0

现在我正在努力。 – dExIT

+0

嗯如何逃脱那些slaashes和\ n的? [[“\”user \“”,“\”actions \“\ n \”user1 \“”,“\”265 \“\ n \”user2 \“”,“\”198 \“\ n” ]] – dExIT

+0

你可以在json中使用str_replace: str_replace(array(“\\”,“\ n”),“”,$ json); 或$字符串(推荐): str_replace函数( '“', ”“,$ JSON); – calexandre

1

你把768,16预计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"} 
] 
*/ 
+0

更新问题 – dExIT

+0

随着你的解决方案,除了PHP警告之外, 你能否看看我更新过的问题。 – dExIT

+0

@dExIT,更新,我没有得到任何警告。 – Danijel

相关问题