dousidan1279 2014-09-28 04:36
浏览 32
已采纳

使用json_decode()获取null

I'm attempting to update records through decoded JSON by ID in this CakePHP function:

public function update() {
    $this->layout = 'ajax';
    if($this->request->is('post')) {
        $decoded = json_decode($this->request->data,true);
        if($data = $this->Foobar->save($decoded)) {
            $data = json_encode(array(
                "message" => "Foobar successfully updated.",
                "update" => $this->request->data
            ));
        } else {
            $data = json_encode(array(
                "message" => "Foobar could not be updated.",
                "update" => $decoded,
                "updateJson" => $this->request->data
            ));
        }
    } else {
        $data = json_encode(array(
            "message" => "Method should be post."
        ));
    }
    $this->set('data', $data);

But the decode keeps returning null:

{"message":"Foobar could not be updated.","update":null,"updateJson":{"ID":"1","status":2}}

However, if I go to http://www.compileonline.com/execute_php_online.php and enter:

<html><head></head><body>
<pre>
<?php
   print_r(json_decode('{"ID":"1","status":2}', true));
?>
</pre>
</body></html>

It works just fine...

Looking at related questions...

  • I've seen suggestions to try json_last_error(), this returns 0 for me.
  • I've seen someone mention magic_quotes might be on, mine are off.
  • I've seen suggestions to use json_decode(utf8_encode($this->request->data),true);, this still returns null for me

Any ideas?

  • 写回答

1条回答 默认 最新

  • doucheng7808 2014-09-28 12:09
    关注

    I don't think you should JSON Decode the data.

    Try to directly save:

    $data = $this->Foobar->save($this->request->data);   
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错