[英]Retrieving data from curl and json_decode
I am trying to extract the individual data strings from a curl and json_decode result, but cannot manage to target the array elements correctly:我试图从 curl 和 json_decode 结果中提取单个数据字符串,但无法正确定位数组元素:
API:应用程序接口:
$ch = curl_init($uri);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('apisign:'.$sign));
$execResult = curl_exec($ch);
$obj = json_decode($execResult);
Result:结果:
{"success":true,"message":"","result":{"Bid":0.04350003,"Ask":0.04395399,"Last":0.04350003}}
PHP: PHP:
echo 'Bid: '.$obj['result']['Bid'].'<br/>';
echo 'Ask: '.$obj['result']['Ask'].'<br/>';
echo 'Last: '.$obj['result']['Last'].'<br/>';
Also tried:还试过:
echo 'Bid: '.$obj['Bid'].'<br/>';
echo 'Ask: '.$obj['Ask'].'<br/>';
echo 'Last: '.$obj['Last'].'<br/>';
You need to add a true parameter to your json_decode您需要在 json_decode 中添加一个 true 参数
Like喜欢
json_decode($execResult, true);
Else you get a stdObject with the decoded data.否则你会得到一个带有解码数据的 stdObject 。
Then you should be able to use $obj['result']['Bid'] aso.那么你应该可以使用 $obj['result']['Bid'] aso。
The use of json_decode
has an optional second parameter - boolean - that either returns the data as an object or as an array. json_decode
的使用有一个可选的第二个参数 - 布尔值 - 将数据作为对象或数组返回。
{"success":true,"message":"","result":{"Bid":0.04350003,"Ask":0.04395399,"Last":0.04350003}}
$obj = json_decode( $execResult ); /* will return an object */
echo $obj->result->Bid;
$obj = json_decode( $execResult, true ); /* will return an array */
echo $obj['result']['Bid'];
Just ran this as the test..刚刚运行这个作为测试..
$data='{"success":true,"message":"","result":{"Bid":0.04350003,"Ask":0.04395399,"Last":0.04350003}}';
$obj = json_decode( $data ); /* will return an object */
echo $obj->result->Bid;
$obj = json_decode( $data, true ); /* will return an array */
echo $obj['result']['Bid'];
which displayed:其中显示:
0.043500030.04350003
Use this way:使用这种方式:
echo 'Bid: '.$obj->result->Bid.'<br/>';
echo 'Ask: '.$obj->result->Ask.'<br/>';
echo 'Last: '.$obj->result->Last.'<br/>';
let让
$json = '{"success":true,"message":"","result":{"Bid":0.04350003,"Ask":0.04395399,"Last":0.04350003}}';
$obj = json_decode($json, true);// decode and convert it in array
echo 'Bid: '.$obj['result']['Bid'].'<br/>';
echo 'Ask: '.$obj['result']['Ask'].'<br/>';
echo 'Last: '.$obj['result']['Last'].'<br/>';
Try using json_decode($string, true);
尝试使用
json_decode($string, true);
as an array作为数组
$ch = curl_init($uri);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('apisign:'.$sign));
$execResult = curl_exec($ch);
$obj = json_decode($execResult, true);
echo 'Bid: '.$obj['result']['Bid'].'<br/>';
echo 'Ask: '.$obj['result']['Ask'].'<br/>';
echo 'Last: '.$obj['result']['Last'].'<br/>';
OR using json_decode($string);
或使用
json_decode($string);
as an object作为对象
$ch = curl_init($uri);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('apisign:'.$sign));
$execResult = curl_exec($ch);
$obj = json_decode($execResult);
echo 'Bid: '.$obj->result->Bid.'<br/>';
echo 'Ask: '.$obj->result->Ask.'<br/>';
echo 'Last: '.$obj->result->Last.'<br/>';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.