duancuisan2503 2017-01-29 21:07
浏览 58
已采纳

如何在PHP中解析JSON对象? 如何检索此JSON对象的某些特定字段的值?

I am absolutly 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 exatly 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? Laravel fframework provides some utility to do it?

Tnx

  • 写回答

2条回答 默认 最新

  • dongmao3148 2017-01-29 21:15
    关注

    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,
        );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图2.0 版本点聚合中Marker的位置无法实时更新,如何解决呢?
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题