简体   繁体   中英

JSON encode and decode on PHP

I have JSON output encoded.

$responseJSON
{"status":1,"content":{"sessionid":"4c86cf1acac07811db6ec670e0b9cdd2"}}

now I do a decode on that

$decoded=json_decode($responseJSON);

print_r($decoded)

I get

stdClass Object (
  [status] => 1 
  [content] => stdClass Object ( 
    [sessionid] => 4c86cf1acac07811db6ec670e0b9cdd2 
  ) 
) 

I don't want decoded like that.

how do I decode to an normal array without those stdClass tag?

Don't have enough rep to comment on other peoples comments

To get the info out after you've processed it with

$decoded = json_decode( $responseJSON, TRUE );

You can access all the information inside of it as normal. do a

var_dump($decoded);

just incase it add's levels you wouldn't expect Then just proceed as usual

echo $decoded['status']
echo $decoded['content']['sessionid']

try

json_decode($responseJSON,true);

the true tells php to generate associative arrays

json_decode second argument can be TRUE which will force all objects to be read in as a PHP associated arrays.

$decoded = json_decode( $responseJSON, TRUE );

PHP.net

When TRUE (referring to the second argument), returned objects will be converted into associative arrays.

采用:

$decoded=json_decode($responseJSON, TRUE);

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