在 PHP 中操作 post json



我正在将json数据从一个php文件发布到另一个php文件。当我尝试访问最终 php 文件中 json 的不同部分时,我收到"非法字符串偏移"警告。

如何访问已发布的 json 数据的不同部分?

这是我的 json:

{
"state": {
"date":"2020-05-14 09:16:01",
"total_cases":"35,903",
"diff_total_cases":"1,091",
"confirmed_deaths":"1,748",
"diff_confirmed_deaths":"54",
"neg_tests":"142,551",
"total_tests":"178,454",
"curr_hospitalized":"1,538",
"diff_hospitalized":"-12",
"total_recovered":"2,569",
"diff_total_recovered":"113",
"active":"31,586"
},
"counties":[array],
"zipcodes": [array]
}

返回的完整 json 可以在此处查看:https://codebeautify.org/jsonviewer/cb49c080

当我编写以下内容时,将返回整个 json:

<?php
$json = json_decode(file_get_contents('php://input'), true);
print_r($json);
?>

但是当我尝试访问数据的任何部分时,不会返回任何内容。这将生成"非法字符串偏移"警告:

<?php
$json = json_decode(file_get_contents('php://input'), true);
$state_date = $json["state"]["date"];
print_r($state_date);
?>

$json = json_decode(json_decode(file_get_contents("php://input")), true);做到了!

最新更新