Convert JSON string to PHP Array

后端 未结 9 1147
渐次进展
渐次进展 2020-12-16 03:43

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\         


        
相关标签:
9条回答
  • 2020-12-16 04:19

    If json_decode isn't working, you could try something like this:

    $arr = explode( '\\",\\"', substr( $json, 3, strlen( $json ) - 3 ) );
    
    0 讨论(0)
  • 2020-12-16 04:20
    $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 ) )
    
    0 讨论(0)
  • 2020-12-16 04:22

    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.

    0 讨论(0)
  • 2020-12-16 04:32
    $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);
    
    0 讨论(0)
  • 2020-12-16 04:34

    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);
    
    0 讨论(0)
  • 2020-12-16 04:43

    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
    ?>
    
    0 讨论(0)
提交回复
热议问题