I have a string like this :
{
"XXX" "XXX"
"XXX" "XXX"
"XXX"
{
"XXX" "XXX"
"XXX" "XXX"
"XXX"
{
"XXX" "XXX"
}
"XXX"
{
"XXX" "XXX"
"XXX" "XXX"
"XXX" "XXX"
"XXX" "XXX"
"XXX" "XXX"
"XXX" "XXX"
"XXX" "XXX"
}
"XXX" "XXX"
}
"XXX"
{
"XXX" "XXX"
"XXX"
{
"XXX" "XXX"
}
"XXX"
{
"XXX" "XXX"
"XXX" "XXX"
"XXX" "XXX"
"XXX" "XXX"
"XXX" "XXX"
"XXX" "XXX"
"XXX" "XXX"
}
"XXX" "XXX"
}
"XXX"
{
"XXX" "XXX"
"XXX"
{
"XXX" "XXX"
}
"XXX"
{
"XXX" "XXX"
"XXX" "XXX"
"XXX" "XXX"
"XXX" "XXX"
"XXX" "XXX"
"XXX" "XXX"
}
"XXX" "XXX"
}
"XXX"
{
"XXX" "XXX"
"XXX"
{
"XXX" "XXX"
"XXX" "XXX"
"XXX" "XXX"
"XXX" "XXX"
"XXX" "XXX"
}
"XXX" "XXX"
}
"XXX"
{
"XXX"
{
"XXX" "XXX"
}
"XXX"
{
"XXX" "XXX"
"XXX" ""
}
"XXX"
{
"XXX" "XXX"
"XXX" ""
}
"XXX"
{
"XXX" "XXX"
"XXX" ""
}
"XXX"
{
"XXX" "XXX"
"XXX" ""
}
"XXX"
{
"XXX" "XXX"
"XXX" ""
}
"XXX"
{
"XXX" "XXX"
"XXX" ""
}
}
}
It looks like JSON, but it isn't : it's lacking all the commas (',') and all the colons (":"). I'm trying to find a way to parse it and turn into a valid JSON string.
My guess is to use the tabulations and the line breaks to find my way in the string, but so far I wasn't able to convert it to valid JSON.
I came up with this, which works but I believe is not very efficient or fail safe :
$e = explode( "
", $string );
foreach( $e as $k => $l ) {
$next = str_replace( "\t", '', $e[ $k + 1 ] );
$isParam = strstr( $l, "\"\t\t\"" );
$currentClean = str_replace( "\t", '', $l );
if ( $isParam )
$e[ $k ] = str_replace( "\"\t\t\"", '":"', $l );
if ( $isParam && $next != '}' )
$e[ $k ] .= ',';
if ( $currentClean == '}' && $next != '}' && $next )
$e[ $k ] .= ',';
if ( preg_match( '#^"(.*)"$#', $currentClean ) && !$isParam )
$e[ $k ] .= ':';
}
$json = implode("
", $e);
How to improve this ?