gpt4 book ai didi

php - 使用 php jsonpath 解析 JSON

转载 作者:可可西里 更新时间:2023-11-01 12:35:21 25 4
gpt4 key购买 nike

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

我的JSON就是来自于这个

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

(此处剪切/粘贴太长,但您可以在浏览器 session 中看到它......)

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, TRUE);
curl_setopt($ch, 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 中使用它,但我不知道如何...任何建议表示赞赏...

注意:我需要使用像 ?(@.descrizione=="A.S.U.I. - Trieste") 这样的 jsonpath 表达式,我不能使用“位置”json 路径 ...

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

建议?

提前谢谢你......

最佳答案

我建议尝试为 JsonPath 使用不同的库,如果您正在使用的库有错误并且第 3 方服务声明查询没有错误。

这里有一些:

我很确定还有更多。希望对您有所帮助。

关于php - 使用 php jsonpath 解析 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45765454/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com