【问题标题】:parse extended json output file from cucumber with php用 php 解析来自黄瓜的扩展 json 输出文件
【发布时间】:2018-01-16 12:37:57
【问题描述】:

我正在尝试解析一个特殊的 json 内容。我从黄瓜执行中得到了这个作为输出文件。目标是解码一些值,如 namestatus 和其他一些内容。我该如何编码。 另一个问题是将 json 转换为 CSV。 对我来说使用 php 并不重要。 Java 或 Perl 将是一个替代方案。 这个文件我要用 php 调用:

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

json 内容是这样的(我只贴了开头):

[
    {
    "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
            }
          }
        ]
      }

【问题讨论】:

    标签: php json parsing cucumber encode


    【解决方案1】:

    用途:

    $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'];
        }
    }
    

    正如其中一个 cmets 所问的,这是访问“默认时间戳开始”的方法:

    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);
                    }
                }
            }
        }
    }
    

    【讨论】:

    • 元素被Gherkin/Cucumber/Ruby放在列中
    • 在我添加了这个代码行之后,会发生什么改变或者我接下来要做什么才能得到我想要的信息?
    • 我检查了您的编辑。首先感谢您的帮助。粘贴您的代码建议后,我得到了这个 PHP 错误:PHP 注意:未定义变量:为 foreach() 提供的数据和无效参数
    • PHP 注意:数组到字符串的转换
    • 所以这个错误是我的。您的代码: $first_element_id = $data[0]['elements'][0]['id'];作品。你能尽快解释一下吗?谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-01
    • 1970-01-01
    • 2022-07-21
    • 1970-01-01
    • 2011-01-11
    相关资源
    最近更新 更多