eval.in

Paste #652483

PHP — PHP 5.6.23, pasted 1 year ago (json)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?php
$json = '{
  "params": {
    "apiKey": "key",
    "sessionId": "123433890",
    "lang": "en",
    "timezone": "America/New_York",
    "query": "hi all",
    "latitude": "37.459157",
    "longitude": "-122.17926",
    "context": "[{"
     name ": "
     weather ","
     lifespan ": 4}]"
  }
}';
// Fix malformed JSON
$json = preg_replace_callback('~"([\[{].*?[}\]])"~s', function ($match) {
	return preg_replace('~\s*"\s*~', "\"", $match[1]);
}, $json);
// Now you can do:
$arr = json_decode($json, true);

var_export ($arr);
?>

Program Output

array (
  'params' => 
  array (
    'apiKey' => 'key',
    'sessionId' => '123433890',
    'lang' => 'en',
    'timezone' => 'America/New_York',
    'query' => 'hi all',
    'latitude' => '37.459157',
    'longitude' => '-122.17926',
    'context' => 
    array (
      0 => 
      array (
        'name' => 'weather',
        'lifespan' => 4,
      ),
    ),
  ),
)

OK (0.008 sec real, 0.008 sec wall, 14 MB, 42 syscalls)

Fork