首页 > 解决方案 > 从第二层/向下嵌套获取 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;

回来好了,所以我知道零件正在工作。我错过了什么?

标签: javascriptphparraysjson

解决方案


推荐阅读