简体   繁体   中英

Convert JSON string to PHP Array

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\"]

I want to convert this to a regular PHP array. I've tried many things but none of them seem to work:

$arrayOfEmails=json_decode($jsonString); //doesn't work
$arrayOfEmails=(array)json_decode($jsonString); //doesn't work

Any ideas?

Edit:

I'm still not getting it to work.

$decodeEmails=$_POST["arrayOfEmails"];
sendResponse(200, $decodeEmails);
//the above returns exactly whats after this colon:[\"a@gmail.com\",\"b@gmail.com\",\"c@gmail.com\]

I need to do this: $arrayOfEmails=json_decode($decodeEmails); But I think I need quotes around $decodedEmails for this to work. How can I add quotes around $decodeEmails string?

试试这个: json_decode($json_string, true);

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);
$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 ) )
    $str=<<<H
    {"a":"AAA","b":"333"}
    H;

     $object = json_decode($str); 
     $array  = json_decode($str , 1 );

    // $arr = get_object_vars( json_decode($str) );

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
?>

如果json_decode不起作用,你可以尝试这样的事情:

$arr = explode( '\\",\\"', substr( $json, 3, strlen( $json ) - 3 ) );

This code works fine.

$jsonString = '["m@gmail.com","b@gmail.com","c@gmail.com"]';
$arrayOfEmails=json_decode($jsonString);
$arrayOfEmails=(array)json_decode($jsonString);
print_r($arrayOfEmails);

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM