使用php jsonpath解析JSON

我正在尝试使用jsonpath解析 PHP中的JSON ….

我的JSON来自于此

https://servizionline.sanita.fvg.it/tempiAttesaService/tempiAttesaPs

(这里剪切/粘贴太长了,但你可以在浏览器会话中看到它……)

JSON是一个有效的JSON(我已经使用https://jsonlint.com/验证了它……).

我已经使用http://www.jsonquerytool.com/尝试了jsonpath表达式,所有看起来都很好,但是当我把所有的PHP代码示例放在下面….

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

    require_once('json.PHP');      // JSON parser
    require_once('jsonpath-0.8.0.PHP');  // JSONPath evaluator

    $url = 'https://servizionline.sanita.fvg.it/tempiAttesaService/tempiAttesaPs';

    $ch = curl_init();
    curl_setopt($ch,CURLOPT_AUTOREFERER,TRUE);
    curl_setopt($ch,CURLOPT_HEADER,0);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_FOLLOWLOCATION,CURLOPT_PROXY,'');
    $data = curl_exec($ch);
    curl_close($ch);

    $parser = new Services_JSON(SERVICES_JSON_LOOSE_TYPE);
    $o = $parser->decode($data);

    $xpath_for_parsing = '$..aziende[?(@.descrizione=="A.S.U.I. - Trieste")]..prontoSoccorsi[?(@.descrizione=="Pronto Soccorso e terapia Urgenza Trieste")]..dipartimenti[?(@.descrizione=="Pronto Soccorso Maggiore")]..codiciColore[?(@.descrizione=="Bianco")]..situazionePazienti..numeroPazientiInAttesa';

    $match1 = jsonPath($o,$xpath_for_parsing);
    //print_r($match1);
    $match1_encoded = $parser->encode($match1);
    print_r($match1_encoded);

    $match1_decoded = json_decode($match1_encoded);

    //print_r($match1_decoded);

    if ($match1_decoded[0] != '') {
     return  $match1_decoded[0];
    }
    else {
     return  "N.D.";
   } 
?>

…没有打印任何值…只是一个“假”值.

当我把它放在我的PHP代码中时,我的jsonpath表达式出了问题:出现的错误如下

Warning: Missing argument 3 for JsonPath::evalx(),called in /var/www/html/OpenProntoSoccorso/Test/jsonpath-0.8.0.PHP on line 84 and defined in /var/www/html/OpenProntoSoccorso/Test/jsonpath-0.8.0.PHP on line 101

Notice: Use of undefined constant descrizione - assumed 'descrizione' in /var/www/html/OpenProntoSoccorso/Test/jsonpath-0.8.0.PHP(104) : eval()'d code on line 1

可能我要逃避/引用我的jsonpath在PHP中使用它但我不知道如何……任何建议都值得赞赏…

注意:我需要使用jsonpath表达式,如?(@ .descrizione ==“A.S.U.I. – Trieste”),我不能使用“位置”json路径…

我也试过使用来自https://github.com/ITS-UofIowa/jsonpath/blob/master/jsonpath.php的jsonpath-0.8.3.PHP,但没有任何改变……

建议?

先感谢您 …

我建议尝试为JsonPath使用不同的库,如果你正在使用的那个有bug,第三方服务在查询中没有说错误.

以下是一些:

> FlowCommunications/JSONPath
> Peekmo/JsonPath

我很确定还有更多.希望有所帮助.

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...