I have the following JSON string, which was an Objective C array then encoded to JSON:
$jsonString=[\\\"a@gmail.com\\\",\\\"b@gmail.com\\\",\\\"c@gmail.com\
If json_decode
isn't working, you could try something like this:
$arr = explode( '\\",\\"', substr( $json, 3, strlen( $json ) - 3 ) );
$data = unserialize($data)
now u can get the $data as array
For example $data have the value like this
$data = "a:2:{s:18:"_1337666504149_149";a:2:{s:8:"fbregexp";s:1:"1";s:5:"value";s:4:"2222";}s:18:"_1337666505594_594";a:2:{s:8:"fbregexp";s:1:"3";s:5:"value";s:5:"45555";}}";
$data = unserialize($data)
now i get value like this
Array ( [fbregexp] => 1 [value] => 2222 ) [_1337666505594_594] => Array ( [fbregexp] => 3 [value] => 45555 ) )
Assuming the lack of quotes around your JSON in the question is a transposition error during posting, then the code you're using is fine: http://codepad.org/RWEYM42x
You do need to ensure your string is UTF8 encoded. You can use the built in encoder if it isn't ( http://php.net/manual/en/function.utf8-encode.php ).
For any further help, you need to actually tell us what you are getting with your code.
$arr = explode( '\\",\\"', substr( $json, 3, strlen( $json ) - 3 ) )
This solution is good but for getting full valid array I'm using strlen( $json ) - 6
, so it should be:
$arr = explode( '\\",\\"', substr( $json, 3, strlen( $json ) - 6 ) );
var_dump($arr);
You should quote your string, it works fine, see here.
$jsonString = '["m@gmail.com","b@gmail.com","c@gmail.com"]';
$arrayOfEmails=json_decode($jsonString);
Or
$jsonString = "[\"a@gmail.com\",\"b@gmail.com\",\"c@gmail.com\"]";
$arrayOfEmails=json_decode($jsonString);
You could use json_decode() then print_r() to create a PHP formatted array
<?php
$json = file_get_contents('json/yourscript.json'); // Get the JSON data
$phpObj = json_decode($json,true); // Convert to PHP Object
print_r($phpObj); // Convert to PHP Array
?>