Reputation: 19380
can anyone recognize this format (if it is a standard format), or should I explode it manually to get arrays? Thanks.
{ coords : {lon : 7.41891, lat : 43.73253}, address : "", zipCode : "98000", city : "Monaco", sK : "Monaco", sQ : "852", fV : "", fZ : "98000 Monaco", fW : "- MC - Monaco: Monaco (98000)", gf : "31NDFzeHoxMGNORE11TnpNeU5UTT1jTnk0ME1UZzVNUT09", reflexId : "84167", areaLabel : "Monaco", jalon : 4}
YAML to VarExport gives this but then fails.
array (
'coords' =>
array (
'lon' => 20.48406,
'lat' => 44.80572,
),
)
Solution:
$data = preg_replace('#([\w]+) :#is', '"$1" :', $data);
Result:
array (
'coords' =>
{
'lon' => 7.41891,
'lat' => 43.73253,
},
'address' => '',
'zipCode' => '98000',
'city' => 'Monaco',
'sK' => 'Monaco',
'sQ' => '852',
'fV' => '',
'fZ' => '98000 Monaco',
'fW' => '- MC - Monaco: Monaco (98000)',
'gf' => '31NDFzeHoxMGNORE11TnpNeU5UTT1jTnk0ME1UZzVNUT09',
'reflexId' => '84167',
'areaLabel' => 'Monaco',
'jalon' => 4,
)
Upvotes: 1
Views: 471
Reputation: 311
After doing some research, it definitely looks like improperly formatted JSON. However, it's not too far off. In fact, with two regular expressions you could easily convert the string to proper JSON.
Find:
/(\w+)\s+\:/
Replace:
"$1" :
Find:
/(})(\s+{)/
Replace:
$1,$2
You can do it like this in PHP:
$good_json = preg_replace(array('/(\w+)\s+\:/i','/(})(\s+{)/'), array('"\1" :', '\1,\2'), $bad_json);
var_dump( json_decode($good_json) );
Upvotes: 0
Reputation: 44346
It's valid JavaScript code and almost JSON. For it to be valid JSON it would need to have the object property names enclosed in double quotes and have the array items separated by commas.
This:
$text = preg_replace('/(\r?\n){2}/', ',', trim($text));
$text = preg_replace('/([{,])\s?([a-z0-9_]+)\s?:/i', '$1"$2":',$text);
print_r(json_decode($text));
will work for your example.
But you should not use it as it will fail when there are ,
or {
inside values!
The safest approach would be to construct a parser yourself.
Upvotes: 2
Reputation: 101936
Bad JSON. I would convert the hash keys to strings and then json_decode
. You can do that with the PHP Tokenizer fairly simply.
function parse($code) {
$result = '';
$tokens = tokens_get_all('<?php ' . $code);
array_shift($tokens); // drop <?php
foreach ($tokens as $token) {
if (!is_array($token)) {
$result .= $token;
continue;
}
if ($token[0] == T_STRING) {
$token[1] = '\'' . addslashes($token[1]) . '\'';
}
$result .= $token[1];
}
return json_decode($result);
}
Upvotes: 0
Reputation: 39695
Looks like json serialization to me, so fairly standard. :)
Upvotes: 0