我有一个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值。
答案 0 :(得分:1)
试试这个
$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;
答案 1 :(得分:0)
$output = json_decode($input);
如果您的JSON是单个对象,则可以使用$output->req_payload->config->action
和$outout->api_url
。如果它是一个数组,那么你将不得不迭代它的元素。如果它就像你在问题中显示的那个,那么它就是无效的JSON。