2017-05-25 86 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

相關問題