我不知道如何提取以下JSON对象。我尝试使用foreach但它不起作用。你可以帮帮我吗? 这里是JSON文件:
{
"success":true,
"moreDataAvailable":true,
"ewons":
[
{
"id":252459,
"name":
"eWON_TEST",
"tags":
[
{
"id":107060,
"name":"TEMP_EXT",
"dataType":"Int",
"description":"Température extérieure",
"alarmHint":"",
"value":15.0,
"quality":"good",
"ewonTagId":1,
"history":
[
{
"date":"2016-05-09T16:06:49Z",
"quality":"initialGood",
"value":64.0
},
{
"date":"2016-05-09T16:16:49Z",
"quality":"initialGood",
"value":6.0
},
{
"date":"2016-05-09T16:21:49Z",
"quality":"initialGood",
"value":6.0
}
]
},
{
"id":107072,
"name":"TEMP_IN",
"dataType":"Int",
"description":"Température intérieure",
"alarmHint":"",
"value":22.0,
"quality":"good",
"ewonTagId":5,
"history":
[
{
"date":"2016-05-09T17:01:49Z",
"quality":"initialGood",
"value":22.0
},
{
"date":"2016-05-09T17:06:49Z",
"value":22.0
},
{
"date":"2016-05-09T17:11:49Z",
"value":22.0
}
]
}
],
"lastSynchroDate":"2016-12-16T15:21:22Z"
}
]
}
事实上,我想显示每个标签的历史记录,每行都有以下信息:
['id'],['name'],['dataType'],['descritption'],['ewonTagId'],['id(from ewons)'],['name(from ewons)'],['date'],['quality(from history)'],['value']
我现在有以下代码,但我没有尝试过循环工作......
<?php
//Read JSON file
$filename ='fichier.json';
$json = file_get_contents($filename);
//convert json object to php associative array
$data = json_decode($json,true);
// Loop trough the array
foreach(???) {
echo ???
}
?>
答案 0 :(得分:0)
你需要三个foreach循环。
//convert json object to php associative array
$data = json_decode($json,true);
// Loop trough the array
foreach($data['ewons'] as $_ewon) {
foreach ($_eown['tags'] as $_tag) {
foreach ($_tag['history'] as $_history) {
// now echo your data from $_ewon, $_tag and_history
// do a var_dump or print_r if you're unsure about what's exactly in these variables
}
}
}