如何在cakePHP 3.x中接收从客户端发送到服务器端的JSON

如何在cakePHP 3.x中接收从客户端发送到服务器端的JSON,cakephp,cakephp-3.0,Cakephp,Cakephp 3.0,我收到的是字符串而不是Json。我正在使用 $this->request->input() 我的添加方法如下: function add() { if (!empty($this->request->input())) { $this->loadModel('crud'); if($this->crud->save( $this->request->input() ) ) {

我收到的是字符串而不是Json。我正在使用
$this->request->input()

我的添加方法如下:

function add()
{
    if (!empty($this->request->input())) {  
        $this->loadModel('crud');
        if($this->crud->save( $this->request->input() ) )
        {  
            $this->Flash->set("Operation Completed.");
            $this->set('message',"Your user data has been saved.");
        }
        else
            $this->set('message',"Error.");
    }  
}

检查一下文件,都解释得很清楚<默认情况下,code>Request::input()以原始字符串格式返回数据,例如,如果需要转换数据,请向方法传递回调以处理该数据

另见

试试这个

$this->crud->save(json_解码($this->request->input())

或者将输入保存到其他变量,然后尝试按如下方式保存该变量

$input = json_decode($this->request->input())
$this->crud->save($input)

这里有问题吗?是的。。请帮帮我,我已经看过了。。但是使用
$this->request->input('json_decode')
返回null,然后PHP认为您的JSON无效,请检查并检查您的数据@JamshaidTariqsending JSON using postman<代码>{“first_name”:“jamshaid”,“last_name”:“tariq”}如果这就是要检索的内容,那么您将不会得到
NULL
,因为这是完全有效的JSON。您应该调试
Request::input()
(无转换)返回的内容,检查空格、隐藏字符等。。。还要确保
json\u decode
函数实际存在。请注意,这确实是一个完全不同的问题,如果您需要调试方面的帮助,那么您应该提出一个新问题并添加适当的细节,或者最好在IRC上提问,在那里您可以进行长时间的“来回”对话@JamshaidTariqupon在回顾我的JSON时,我知道我正在尝试使用单引号而不是双引号发送JSON。现在
$this->request->input('json_decode')返回一个对象。但当我使用
$this->crud->save($this->request->input('json_decode'))
语句时,对未定义的方法stdClass::errors()的调用出错。
$input = json_decode($this->request->input())
$this->crud->save($input)