PHP,
pasted
on Aug 22:
|
<?php
function jsonify_chunkies($string) {
$out = array();
$lines = preg_split('/\r\n|\r|\n/', $string);
foreach ($lines as $i => $line) {
$out[] = '"' . $i . '": ' . $line . "";
}
return "{\n" . join(",\n", $out) . "\n}";
}
$server_response = '[ "1", "Apples", "Fruit" ]' . "\n" .
'[ "1", "Banannas", "Fruit" ]' . "\n" .
'[ "1", "Pears", "Fruit" ]' . "\n" .
'[ "1", "Potatoes", "Veg" ]';
echo jsonify_chunkies($server_response);
|
Output:
|
{
"0": [ "1", "Apples", "Fruit" ],
"1": [ "1", "Banannas", "Fruit" ],
"2": [ "1", "Pears", "Fruit" ],
"3": [ "1", "Potatoes", "Veg" ]
}
|
|