javascript - 从第二层/向下嵌套获取 json 数据
问题描述
我需要对 .js 页面中 json 数组中的一个对象执行 PHP if 语句。
https://haspdenbloodglucose.herokuapp.com/api/v1/status.js页面的输出给了我"units":"mmol"
,我需要检查这是mmol
还是mgdl
来自另一个页面,然后在需要时转换单位。我在考虑我的 if 语句需要sgv = 100
if ($units == "mmol") {
$sgv = $sgv/18; // converts the units to mmol/l from mg/dl
$sgv = round($sgv, 1); // round to 1 decimal place
} else {
}
我相当有信心,但是$units
从该 json 数组中获取“单位”的值或专门设置的值是另一回事。
它看起来在第二个巢穴中(如果那是正确的词)但是我以前从未见过开头{
之前的文字。
所以我会从一个文件开始获取内容:
$status = file_get_contents('https://haspdenbloodglucose.herokuapp.com/api/v1/status.js' );
但我可以做一个
$json = json_decode($status);
当我尝试
echo $json;
测试这个已经解码它返回空白?
然而
echo $status;
回来好了,所以我知道零件正在工作。我错过了什么?
解决方案
推荐阅读
- r - 如何计算 R quosure 中的变量数?
- python - 我的代码在运行时会打印它之前的输出。我希望它只打印该特定计数所需的内容
- html - 为什么 table-layout: fixed 会影响父元素的宽度?
- html - 通过调用css中的类来更改父级中子级的字体颜色和高度
- c++ - 如何将使用 ExternalProject_Add 安装的库添加到目标包括
- sql - 时区 Rails Postgres
- java - 带有 DBRef 的 Mongo 文档
- ruby - 如何从 BasicObject 中访问 Ruby 类型名称
- javascript - 数组元素的依从性属性 i
- angular - 如何为电子邮件制作输入掩码?