我正在尝试在我的yii2应用中解码JSON,我从一个API获取数据。我得到了这些信息并使用var_dump来查看它。看起来像这样:
yii\ httpclient\ Response Object([client] => yii\ httpclient\ Client Object([baseUrl] => [formatters] => Array([urlencoded] => yii\ httpclient\ UrlEncodedFormatter Object([encodingType] => 1[charset] => ))[parsers] => Array()[requestConfig] => Array()[responseConfig] => Array()[contentLoggingMaxSize] => 2000[_transport: yii\ httpclient\ Client: private] => yii\ httpclient\ StreamTransport Object([_events: yii\ base\ Component: private] => Array()[_behaviors: yii\ base\ Component: private] => )[_events: yii\ base\ Component: private] => Array()[_behaviors: yii\ base\ Component: private] => Array())[_headers: yii\ httpclient\ Message: private] => Array([0] => HTTP / 1.1 200 OK[1] => Server: nginx[2] => Date: Mon, 31 Oct 2016 13: 49: 35 GMT[3] => Content - Type: application / json; charset = UTF - 8[4] => Connection: close[5] => Strict - Transport - Security: max - age = 31536000)[_cookies: yii\ httpclient\ Message: private] => [_content: yii\ httpclient\ Message: private] => [{
"ccy": "EUR",
"base_ccy": "UAH",
"buy": "28.10000",
"sale": "28.40000"
}, {
"ccy": "RUR",
"base_ccy": "UAH",
"buy": "0.40000",
"sale": "0.42000"
}, {
"ccy": "USD",
"base_ccy": "UAH",
"buy": "25.70000",
"sale": "25.90000"
}, {
"ccy": "BTC",
"base_ccy": "USD",
"buy": "665.7850",
"sale": "735.8676"
}][_data: yii\ httpclient\ Message: private] => [_format: yii\ httpclient\ Message: private] => [_events: yii\ base\ Component: private] => Array()[_behaviors: yii\ base\ Component: private] => )
在控制器中:
public function actionRates()
{
$model = new ExchangeRates();
$data = $model->getRate();
$goodData = json_decode($data, true);
return $this->render('about',['data' => $goodData]);
}
在视图中:
<?php var_dump($data) ?>
但是当我尝试使用json_decode并通过var_dump时,看到我有NULL。我看到了这个问题的不同解决方案,但没有人帮助我:(