【问题标题】:JSON complex tree with PHP [duplicate]带有PHP的JSON复杂树[重复]
【发布时间】:2017-05-05 14:37:13
【问题描述】:

我不知道如何提取以下 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 ???
}
?>

【问题讨论】:

  • 我今天早上试了一下,但没有成功:-(
  • 这个问题必须有 100 个可能的重复项。在 JSON 标记中进行一些搜索

标签: php json foreach


【解决方案1】:

你需要三个 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
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-12
    • 2012-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-08
    • 2020-12-21
    相关资源
    最近更新 更多