解析json_decode输出

时间:2013-09-22 18:46:35

标签: php json

我在解析json输出时有点困惑。我和json一起盲目工作并尝试了一些教程,但似乎无法找到我正在寻找的东西。

我正在尝试使用正确执行功能的服务提供程序API。然后我以json格式从提供者那里得到正确的反馈。

到目前为止我的代码看起来像;

$response = curl_exec($apicall);
$json_output = json_decode($response);
var_dump($json_output);

然后返回;

object(stdClass)#1 (2) {
["status"]=>
string(2) "OK"
["droplet"]=>
object(stdClass)#2 (5) {
["id"]=>
int(490021)
["name"]=>
string(20) "test.mydomain.com"
["image_id"]=>
int(374535)
["size_id"]=>
int(66)
["event_id"]=>
int(6403716)
  }
}

我正在寻找的方法是将“状态”中的“确定”和“身份”中的“490021”存储为变量。

希望有人可以提供帮助。

1 个答案:

答案 0 :(得分:4)

无需将它们存储为变量。当您使用json_decode时,它会创建一个stdClass,您可以访问这些变量,例如:

$json_output = json_decode($response);
echo $json_output->id;