使用php从黄瓜解析扩展的json输出文件

时间:2018-01-16 12:37:58

标签: php json parsing cucumber encode

我想解析一个特殊的json内容。我把它作为黄瓜执行的输出文件。目标是解码一些值,例如名称 状态和其他一些内容。我怎么能编码呢。 另一个问题是将json转换为CSV。 使用php对我来说并不重要。 Java或Perl将是另一种选择。 这个文件我将用php调用:

$jsonconntent = file_get_contents("/home/xxx/test1.json");

json conten看起来像这样(我只发布了开头):

[
    {
    "uri": "features/complete_ski.feature",
    "id": "complete_ski_with_time",
    "keyword": "Feature",
    "name": "Complete_Ski_with_time",
    "description": "",
    "line": 1,
    "elements": [
    {
        "id": "complete_ski_with_time;time_part_preamble",
        "keyword": "Scenario",
        "name": "time_part_preamble",
        "description": "",
        "line": 3,
        "type": "scenario",
        "before": [
        {
            "output": [
              "Default Timestamp start: 1516024716000"
            ],
            "match": {
              "location": "features/support/env.rb:32"
            },
            "result": {
              "status": "passed",
              "duration": 191690
            }
        },
        {
            "match": {
              "location": "capybara-2.17.0/lib/capybara/cucumber.rb:13"
        },
        "result": {
          "status": "passed",
          "duration": 52117
        }
      },
      {
        "match": {
          "location": "capybara-2.17.0/lib/capybara/cucumber.rb:21"
        },
        "result": {
          "status": "passed",
          "duration": 25885
        }
      }
    ],
    "steps": [
      {
        "keyword": "Given ",
        "name": "a Android A-Party",
        "line": 4,
        "output": [
          "Got handset with number unvisable, IMSI: notfor, android-Id: yourfone, VNC: 11111, port: 9981"
        ],
        "match": {
          "location": "features/step_definitions/idrp_steps.rb:11"
        },
        "result": {
          "status": "passed",
          "duration": 1415024760
        },
        "after": [
          {
            "match": {
              "location": "features/support/env.rb:24"
            },
            "result": {
              "status": "passed",
              "duration": 264339
            }
          }
        ]
      }

1 个答案:

答案 0 :(得分:0)

使用:

$data = json_decode($jsonconntent, true);

您将把javascript对象的数据作为数组,而不是PHP对象。

要将其另存为CSV,它取决于数据的结构。在这种情况下,它很复杂:

{
  "uri": "features/complete_ski.feature",
  "id": "complete_ski_with_time",
  "keyword": "Feature",
  "name": "Complete_Ski_with_time",
  "description": "",
  "line": 1,
  "elements": [
    {
      "id": "complete_ski_with_time;time_part_preamble",
      "keyword": "Scenario",
      "name": "time_part_preamble",
      "description": "",
      "line": 3,
      ...

如何将数据放入elements列?数据是如此嵌套,以至于无法将其转换为带有列标题的表格格式,并且每行中每列都有一个值。

关于如何访问数据,您可以这样做:

$first_element_id = $data[0]['elements'][0]['id'];

foreach ( $data as $item ) {
    $uri = $item['uri'];
    foreach ( $item['elements'] as $element ) {
        $name = $element['name'];
    }
}

正如其中一条评论中所述,这是如何访问'默认时间戳开始':

foreach ($data as $item) {
    foreach ($item['elements'] as $element) {
        foreach ($element['before'] as $before) {
            if (isset($before['output'])) {
                foreach ($before['output'] as $output) {
                    echo sprintf("%s\n", $output);
                }
            }
        }
    }
}