我有一个多级JSON文件,我试图从中获取特定值,但我尝试的所有内容似乎都不起作用。
JSON:
{
"mapVersion":59,
"mapVersionString":"Fixed \"Me\" button. Fixed live updating of markers, Improved map performance...",
"results":[
{
"ID":3644,
"ParentID":null,
"THINGSPEAK_PRIMARY_ID":"340896",
"THINGSPEAK_PRIMARY_ID_READ_KEY":"ZGHNWCPCHD3HCNQM",
"Label":"PurpleAir - Copperwood",
"Lat":33.587254,
"Lon":-112.196945,
"PM2_5Value":"30.93",
"State":null,
"Type":"PMS5003+PMS5003+BME280",
"Hidden":"false",
"Flag":null,
"DEVICE_LOCATIONTYPE":"outside",
"DEVICE_BRIGHTNESS":"15",
"isOwner":0,
"A_H":null,
"temp_f":"59",
"humidity":"27",
"pressure":"978.36",
"AGE":0,
"THINGSPEAK_SECONDARY_ID":"340899",
"THINGSPEAK_SECONDARY_ID_READ_KEY":"CUMUMPCSVJM1L47F",
"LastSeen":1514524202,
"Version":"2.49j",
"LastUpdateCheck":1514522192,
"Uptime":"81220",
"RSSI":"-64",
"Stats":"{\"v\":30.93,\"v1\":32.82,\"v2\":34.24,\"v3\":33.03,\"v4\":20.49,\"v5\":22.6,\"v6\":21.8,\"pm\":30.93,\"lastModified\":1514524202452,\"timeSinceModified\":80183}"
},
{
"ID":3645,
"ParentID":3644,
"THINGSPEAK_PRIMARY_ID":"340901",
"THINGSPEAK_PRIMARY_ID_READ_KEY":"VYY92GGQW8EAHU7F",
"Label":"PurpleAir - Copperwood B",
"Lat":33.587254,
"Lon":-112.196945,
"PM2_5Value":"31.63",
"State":null,
"Type":null,
"Hidden":"false",
"Flag":null,
"DEVICE_LOCATIONTYPE":null,
"DEVICE_BRIGHTNESS":null,
"isOwner":0,
"A_H":null,
"temp_f":"59",
"humidity":"27",
"pressure":"978.39",
"AGE":0,
"THINGSPEAK_SECONDARY_ID":"340903",
"THINGSPEAK_SECONDARY_ID_READ_KEY":"D5WDUE4DEKX6RNVW",
"LastSeen":1514524232,
"Version":"2.49j",
"LastUpdateCheck":null,
"Uptime":"81250",
"RSSI":"-64",
"Stats":"{\"v\":31.63,\"v1\":32.95,\"v2\":34.32,\"v3\":32.98,\"v4\":20.3,\"v5\":22.58,\"v6\":21.46,\"pm\":31.63,\"lastModified\":1514524232224,\"timeSinceModified\":79975}"
}
]
}
我正试图达到统计数据下的“v”值,但我对如何做到这一点感到茫然。我已经json_decode
尝试了$json_result->results->stats;
,但我得到的只是一个空白的回报,所以我不知道我做错了什么(或没做错)。
任何帮助总是一个受欢迎的学习机会!
答案 0 :(得分:1)
试试这个。您缺少result[0]
索引号
$json_result=json_decode($a);
echo "<pre>";
print_r($json_result->results[0]->Stats);
答案 1 :(得分:1)
结果包含两个数组条目,统计信息区分大小写。
php > var_dump($json_result->results[0]->Stats);
string(141) "{"v":30.93,"v1":32.82,"v2":34.24,"v3":33.03,"v4":20.49,"v5":22.6,"v6":21.8,"pm":30.93,"lastModified":1514524202452,"timeSinceModified":80183}"
此外,如您所见,Stats包含JSON,需要对其进行解码:
php > $json_result->results[0]->Stats = json_decode($json_result->results[0]->Stats);
php > var_dump($json_result->results[0]->Stats);
object(stdClass)#4 (10) {
["v"]=>
float(30.93)
["v1"]=>
float(32.82)
["v2"]=>
float(34.24)
["v3"]=>
float(33.03)
["v4"]=>
float(20.49)
["v5"]=>
float(22.6)
["v6"]=>
float(21.8)
["pm"]=>
float(30.93)
["lastModified"]=>
int(1514524202452)
["timeSinceModified"]=>
int(80183)
}
php > var_dump($json_result->results[0]->Stats->v);
float(30.93)