The problem your having is this string is NOT Proper JSON: [500,'hello world']
this would be proper JSON [500,"hello world"]
. JSON is very strict on formatting and requires that all string values be wrapped in double quotes and NEVER single quotes.
the proper thing to do to avoid problems, would be to use the php functions json_encode()
and json_decode()
for example,
<?php
$myarray[] = 500;
$myarray[] = "hello world";
$myjson = json_encode($myarray);
?>
<form name="input" action="post.php">
<input type="hidden" name="json" value="<?php echo $myjson ?>" />
<input type="submit" value="Submit">
</form>
and in the post.php you would read it like so,
<?php
$posted_data = array();
if (!empty($_POST['json'])) {
$posted_data = json_decode($_POST['json'], true);
}
print_r($posted_data);
?>
the true
flag in json_decode()
tells the function you want it as an associative array and not a PHP object, which is its default behavior.
the print_r()
function will output the php structure of the converted JSON array:
Array(
[0] => 500
[1] => hello world
)