【问题标题】:Parsing JSON using Skyscanner JsonPath implementation in PHP在 PHP 中使用 Skyscanner JsonPath 实现解析 JSON
【发布时间】:2017-09-01 19:38:51
【问题描述】:

我正在尝试使用 Skyscanner JsonPath 实现(参考 https://github.com/Skyscanner/JsonPath-PHP)。

按照他们的指示,我尝试以这种方式制作一个小的 PHP 示例(名为 testSkyScanner.php)(我使用的是 Ubuntu 15.10 ....)

<?php
    ini_set('display_errors', 'On');
    error_reporting(E_ALL);

    use JsonPath/JsonObject

    $theJson = '{
        "codiciColore": [{
            "id": 2,
            "descrizione": "Giallo",
            "rgb": "FFFF00",
            "priorita": 2,
            "situazionePazienti": {
                "numeroPazienti": 9,
                "numeroPazientiInVisita": 9,
                "mediaAttesa": "00:22",
                "numeroPazientiInAttesa": 0
            }
        }, {
            "id": 3,
            "descrizione": "Verde",
            "rgb": "00FF00",
            "priorita": 3,
            "situazionePazienti": {
                "numeroPazienti": 16,
                "numeroPazientiInVisita": 9,
                "mediaAttesa": "03:03",
                "numeroPazientiInAttesa": 7
            }
        }]
   }';

   $jsonObject = new JsonObject();

?>

我已经以这种方式组织了我的代码......

当我尝试使用php testSkyScanner.php 执行它时,我收到此错误...

PHP Warning:  The use statement with non-compound name 'JsonPath' has no effect in /var/www/html//Test/tmp/MyTestSkyScanner/testSkyScanner.php on line 7
PHP Parse error:  syntax error, unexpected '/', expecting ',' or ';' in /var/www/html/Test/tmp/MyTestSkyScanner/testSkyScanner.php on line 7

建议?提前致谢!

【问题讨论】:

    标签: php json parsing


    【解决方案1】:
    PHP Warning:  The use statement with non-compound name 'JsonPath' has no effect in /var/www/html//Test/tmp/MyTestSkyScanner/testSkyScanner.php on line 7
    

    这个错误告诉你

    use JsonPath/JsonObject
    

    没有取得任何成果(而且也是无效的,见下文)。 PHP 中没有“导入的命名空间”的概念。 use 关键字用于创建别名(有助于长命名空间),但在使用类时必须提供完全限定的命名空间。

    PHP Parse error:  syntax error, unexpected '/', expecting ',' or ';' in /var/www/html/Test/tmp/MyTestSkyScanner/testSkyScanner.php on line 7
    

    命名空间使用反斜杠而不是正斜杠分隔,这就是导致此错误的原因。

    您应该完全省略“使用”行,而是调用:

    $jsonObject = new JsonPath\JsonObject();
    

    如果您使用 Composer,请不要忘记在脚本顶部要求 composer 自动加载脚本:

    require __DIR__ . '/vendor/autoload.php';
    

    【讨论】:

    • 我尝试评论“use”行并使用$jsonObject = new JsonPath/JsonObject();,但获得....PHP Fatal error: Class 'JsonPath' not found in /var/www/html/Test/tmp/MyTestSkyScanner/testSkyScanner.php on line 40 ....(我是PHP的新手,很抱歉入门级问题.....)
    • 反斜杠,而不是正斜杠 - 你真的在任何地方都需要()或包含()模块吗?
    • 你是用composer来获取库的吗?如果是这样,您需要要求自动加载文件。我将编辑我的答案以包含此
    • 是的,我用过 composer .....这对我的 PHP 技能来说相当困难,但现在,有了你的回复,我有工作要做......
    • 很高兴为您提供帮助。如果答案对您有用,请记住接受它。 :)
    猜你喜欢
    • 2018-01-27
    • 1970-01-01
    • 2022-01-03
    • 2017-02-05
    • 1970-01-01
    • 2021-09-18
    • 1970-01-01
    • 2023-02-23
    • 2016-02-05
    相关资源
    最近更新 更多