简体   繁体   中英

How can I parse a JSON object in PHP? How can I retrieve the values of some specific fields of this JSON object?

I am absolutely new in PHP and moreover in Laravel framework (I don't know if Laravel provides some utility class for this kind of tasks). I came from Java.

So I have the following problem:

Into a class I perform a call to a REST web service, something like this:

    $response = $client->get('http://localhost:8080/Extranet/login',
        [
            'auth' => [
                'dummy@gmail.com',
                'pswd'
            ]
        ]);

    $dettagliLogin = json_decode($response->getBody());

   \Log::info('response: '.(json_encode($dettagliLogin)));

$response->getBody() contains the returned JSON object, this is the output of the previous \\Log::info() :

{
    "id":5,
    "userName":"Dummy User",
    "email":"dummy@gmail.com",
    "enabled":true
} 

So I have the following problems:

1) What exactly returns the json_decode() function? I really can't understand because PHP is not strongly typed and I have not a declared return type.

This is the method signature:

function json_decode($json, $assoc = false, $depth = 512, $options = 0)

and in the related doc it says @return mixed . What exactly means "mixed"?

2) Anyway the main problem is: I have to use the content of the previous returned JSON object and put these value into the related field of an array like this:

$attributes = array(
    'id' => HERE THE id FIELD VALUE OF MY JSON OBJECT,
    'username' => HERE THE email FIELD VALUE OF MY JSON OBJECT',
    'name' => HERE THE userName FIELD VALUE OF MY JSON OBJECT,
    );

So I think that I have to parse the value of the $response->getBody() or of the json_decode($response->getBody()) to obtain these values. But how exactly can I do it? What is the neater way to do it? Does the Laravel framework provide some utility to do it?

For better understanding, let's first describe - what's JSON? It's a way of representing objects (arrays, objects, etc) in a string.

1) What exactly returns the json_decode() function? I really can't understand because PHP is not strongly typed and I have not a declared return type. This is the method signature:

function json_decode($json, $assoc = false, $depth = 512, $options = 0) and in the related doc it says @return mixed. What exatly means mixed?

json_deocde converts the JSON string into the original "structure" it represent.

@return mixed means that the returned value of json_decode can be any type of variable. If the JSON represent an array - it would be an array type, if it represent an object - it would be an object type.

2) Anyway the main problem is: I have to use the content of the previous returned JSON object and put these value into the related field of an array like this:

$attributes = array( 'id' => HERE THE id FIELD VALUE OF MY JSON OBJECT, 'username' => HERE THE email FIELD VALUE OF MY JSON OBJECT', 'name' => HERE THE userName FIELD VALUE OF MY JSON OBJECT, );

In order to make sure which type of variable your JSON represent, you can use var_dump(json_decode($json)); . Anyway, it's a class object.

Therefore:

$object = json_decode($json);

$attributes = array(
    'id' => $object->id,
    'username' => $object->email,
    'name' => $object->userName,
    );

If you json string is an object (not an array) it will return an object (of type stdClass ). Mixed means it can be multiple things, so if it was a json array, you'd get an array.

Best thing to do is use json_decode , and then var_dump (or var_export ) to see what you actually get.

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