2015-10-05 99 views
1

我在将某些字符串转换为Json时遇到了一些错误。我附上下面的字符串。在PHP中将分号分隔的字符串转换为JSON

si;dialed_no;connect_time;duration;region;call_cost 
0;918592877727;2015-08-25 18:51:01;21;India(91);0.029 
1;918907777727;2015-08-25 19:04:08;220;India(91);0.232 
2;918907777727;2015-08-25 19:09:50;40;India(91);0.058 
3;918907777727;2015-08-25 19:10:46;69;India(91);0.087 
4;919048232151;2015-08-26 13:30:24;19;India(91);0.029 
5;919895842822;2015-08-26 14:23:35;423;India(91);0.435 

编辑1:哎呀。直到现在忘了添加代码。不管怎样附上我的代码到目前为止

function my_wrap($val) { 
    return '{"test":"' . $val. '"}'; 
} 

$parts = explode(';', $string); 
$parts = array_map('my_wrap', $parts); 
$json = '[' . implode(',', $parts) . ']'; 

echo $json; 

和输出就像

[{"test":"dialed_no"},{"test":"connect_time"},{"test":"duration"},{"test":"region"},{"test":"call_cost 0"},{"test":"918592877727"},{"test":"2015-08-25 18:51:01"},{"test":"21"},{"test":"India(91)"},{"test":"0.029 1"},{"test":"918907777727"},{"test":"2015-08-25 19:04:08"},{"test":"220"},{"test":"India(91)"},{"test":"0.232 2"},{"test":"918907777727"},{"test":"2015-08-25 19:09:50"},{"test":"40"},{"test":"India(91)"},{"test":"0.058 3"},{"test":"918907777727"},{"test":"2015-08-25 19:10:46"},{"test":"69"},{"test":"India(91)"},{"test":"0.087 4"},{"test":"919048232151"},{"test":"2015-08-26 13:30:24"},{"test":"19"},{"test":"India(91)"},{"test":"0.029 5"},{"test":"919895842822"},{"test":"2015-08-26 14:23:35"},{"test":"423"},{"test":"India(91)"},{"test":"0.435 6"},{"test":"8801711788025"},{"test":"2015-08-30 19:29:48"},{"test":"1"},{"test":"Bangladesh(880)"},{"test":"0.029 7"},{"test":"8801711788025"},{"test":"2015-08-30 19:29:57"},{"test":"2"},{"test":"Bangladesh(880)"},{"test":"0.029 8"},{"test":"8801711788025"},{"test":"2015-08-30 19:30:07"},{"test":"2"},{"test":"Bangladesh(880)"},{"test":"0.029 9"},{"test":"8801711788025"},{"test":"2015-08-30 19:30:17"},{"test":"1"},{"test":"Bangladesh(880)"},{"test":"0.029 10"},{"test":"8801711788025"},{"test":"2015-08-30 21:24:31"},{"test":"88"},{"test":"Bangladesh(880)"},{"test":"0.087 11"},{"test":"8801833316038"},{"test":"2015-08-31 12:06:15"},{"test":"5"},{"test":"Bangladesh(880)"},{"test":"0.029 12"}] 

我想要的东西是什么样子,

[{si:"0",dialed_no:"91xxx",connect_time:"2015-08-25 18:51:01"}, {si:"1",dialed_no:"9184sd",connect_time:"2015-08-25 18:51:01"}] 

等等...

注意:我从API URL获取上述输入字符串,而不是从csv文件或其他东西。

+3

*面临着一些错误*你怎么能得到错误,而无需代码? – Rizier123

+0

你如何得到这个可以发布该代码? –

+0

更新了所需信息的帖子... –

回答

0

你可以试试这段代码,看看它是否可以正常工作吗?

//we split the single lines 
$lines = explode("\n", $string); 

$linesArray = array(); 

//we split each line in a set of elements 
foreach($lines as $line){ 
    $linesArray[] = explode(";",$line); 
} 
//we use the first line of data as an array of headers 
$headers = $linesArray[0]; 
//and remove it 
unset($linesArray[0]); 

$jsonArray = []; 

foreach($linesArray as $l=>$ln){ 
    foreach($ln as $k=>$part){ 
     //we re-build an array with the right headers 
     $jsonArray[$l][$headers[$k]] = $part; 
    } 
} 

print json_encode($jsonArray); 
+0

感谢您的回答。它的作品像魅力.... –

0

你所描述的你想要的输出不是JSON。

当PHP已经有完美的例程(例如用于JSON编码和CSV解析)时,不要发明自己的例程。

假设数据以文件开始....

$data=array(); 
$y=0; 
$header=fgtetcsv($file_handle, 0, ';'); 
while (!feof($file_handle)) { 
    $row=fgtetcsv($file_handle, 0, ';'); 
    foreach ($row as $x=>$value) { 
     $data[$y][$header[$x]]=$value; 
    } 
    $y++; 
} 
print json_encode($data); 

当然,这将需要一些调整,以处理错误情况,并可能对数据集比PHP的工作存储器中大。

+0

我从API URL获取上述输入字符串,但是来自URL的响应不是JSON,它与上面提到的纯文本类似。所以我想先将它转换成JSON,然后在我的代码的其他部分重用它。 –

0
<?php 
$records = array_map(
    function($e) { // 2: to each line/record apply this functions 
     return str_getcsv($e, ';'); // 3: split the line/record into fields 
    }, 
    explode("\n", data()) // 1: split data into "lines"/records 
); 
// 4: now $records is an array of records, each being an array of fields 
$fields = array_shift($records); // 5: first record contains the field names, remove from array and assign to $fields 
$records = array_map(
    function($e) use ($fields) { // 7: this function has access to $fields, i.e. the names of the fields 
     return array_combine($fields, $e); // 8: see http://docs.php.net/array_combine 
    }, 
    $records // 6: apply the function above to each element, i.e. record, in $records 
); 

echo json_encode($records); 




function data() { 
    return <<< eot 
si;dialed_no;connect_time;duration;region;call_cost 
0;918592877727;2015-08-25 18:51:01;21;India(91);0.029 
1;918907777727;2015-08-25 19:04:08;220;India(91);0.232 
2;918907777727;2015-08-25 19:09:50;40;India(91);0.058 
3;918907777727;2015-08-25 19:10:46;69;India(91);0.087 
4;919048232151;2015-08-26 13:30:24;19;India(91);0.029 
5;919895842822;2015-08-26 14:23:35;423;India(91);0.435 
eot; 
} 
+0

哇你有任何参考在哪里解释如何“使用”像你在那个匿名函数中工作? – nowhere

+0

它更多的是一个列表;-)它将包括:http://docs.php.net/functions.anonymous,https://wiki.php.net/rfc/closures和http://stackoverflow.com/questions/1065188/in-php-5-3-0-what-is-function-use-identifier – VolkerK

+0

我不敢相信我从来没有用过这种有用的东西!感谢您的链接! – nowhere

相关问题