如何从PHP中的json响应中通过键提取值

use*_*208 7 php json

我正在使用getResponse api获取订阅者的最新信息.这是什么印刷后var_dump($result);

object(stdClass)#2 (1) {
  ["updated"]=>
  int(1)
}
Run Code Online (Sandbox Code Playgroud)

如何提取/解码/编码结果以请求密钥:"更新"并获得它的值:1?

谢谢

小智 17


    // json object.
    $contents = '{"firstName":"John", "lastName":"Doe"}';

    // Option 1: through the use of an array.
    $jsonArray = json_decode($contents,true);

    $key = "firstName";

    $firstName = $jsonArray[$key];


    // Option 2: through the use of an object.
    $jsonObj = json_decode($contents);

    $firstName = $jsonObj->$key;