php - 使用 json_decode 后 PHP 中的 print_r 在屏幕上返回“stdClass Object”
问题描述
<?php
$content = file_get_contents("https://min-api.cryptocompare.com/data/price?fsym=BTC&tsyms=USD");
$result = json_decode($content);
print_r ( $result);
?>
所以我创建了那段代码,但是当我去测试它时,它会在屏幕上返回“stdClass Object ([USD] => 7531.74)”。我想要的只是 7531.74,我该如何去掉它的其余部分?
解决方案
您可以执行以下两项操作之一:
// as a stdClass Object
$temp = json_decode($content);
$result = $temp->USD ;
或者,使用数组:
// as an associative Array
$temp = json_decode($content,true);
$result = $temp['USD'] ;
推荐阅读
- php - 使用模板时如何在新页面中插入内容?
- ansible - Ansible:如何使用通配符或替代方法?
- powershell - Powershell 作业活动目录超时
- android - Firebase 云函数 - 对象不能用 JSON 编码 - Kotlin 数据类
- nuxt.js - 如何在生产服务器上运行 nuxt dev
- java - Azure blobs Java sdk 无法返回对象的 versionID 列表
- c# - 从格式 dd.MM.yyyy hh:mm:ss 将字符串解析为 .netcore 中的日期时间
- arrays - 将数组结构的元素与指向该结构的指针相加
- c++ - 使用带有一些自定义 bnf 规则的 Boost Spirit 解析 BNF 语法
- node.js - deleteMany 仅返回在更改流中删除的 1 个值