Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何通过php从json获取数据?_Php_Json - Fatal编程技术网

如何通过php从json获取数据?

如何通过php从json获取数据?,php,json,Php,Json,我有一个json上下文,我想要一些来自json的数据,我的json是 { "req_payload": "{\"config\": {\"action\": \"test\", \"user_id\": \"219096271813\", \"endpoint_url\": \"http:\/\/debasisa.testbase.info\/demo-push.php\", \"webhook_id\": \"430339\"}, \"api_url\": \"https:\/\/www.de

我有一个json上下文,我想要一些来自json的数据,我的json是

{
"req_payload": "{\"config\": {\"action\": \"test\", \"user_id\": \"219096271813\", \"endpoint_url\": \"http:\/\/debasisa.testbase.info\/demo-push.php\", \"webhook_id\": \"430339\"}, \"api_url\": \"https:\/\/www.demo.com\/{api-endpoint-to-fetch-object-details}\/\"}"
}
{
"req_payload": "{\"config\": {\"action\": \"test\", \"user_id\": \"219096271813\", \"endpoint_url\": \"http:\/\/debasisa.testbase.info\/demo-push.php\", \"webhook_id\": \"430339\"}, \"api_url\": \"https:\/\/www.demo.com\/{api-endpoint-to-fetch-object-details}\/\"}"
}
我需要来自json的操作api_url数据。我试着做点什么,然后就成功了

 {"config": {"action": "test", "user_id": "219096271813", "endpoint_url": "http://debasisa.testbase.info/demo-push.php", "webhook_id": "430339"}, "api_url": "https://www.dem.com/{api-endpoint-to-fetch-object-details}/"}
但我不知道如何从PHP中的内容中获取操作和api_url值

试试这个

$level1 = '{
"req_payload": "{\"config\": {\"action\": \"test\", \"user_id\": \"219096271813\", \"endpoint_url\": \"http:\/\/debasisa.testbase.info\/demo-push.php\", \"webhook_id\": \"430339\"}, \"api_url\": \"https:\/\/www.demo.com\/{api-endpoint-to-fetch-object-details}\/\"}"
}';
$data = json_decode($level1);
$newdata = json_decode($data->req_payload);
$action = $newdata->config->action;

如果JSON是单个对象,则可以使用
$output->req\u payload->config->action
$outout->api\u url
。如果它是一个数组,那么您必须迭代它的元素。如果它与您在问题中显示的一样,那么它是无效的JSON。

JSON\u decode
可以帮助您。可能重复Thanx@urfusion,我使用json_解码,但我得到{“config”:{“action”:“test”,“user_id”:“219096271813”,“endpoint_url”:“webhook_id”:“430339”},“api_url”:“{api endpoint to fetch object details}/”,如何从该站点获取操作和api_urlcontent@Debasish:检查答案。
$output = json_decode($input);