dongtou2097 2013-12-07 18:48
浏览 40
已采纳

如何JSON解码codeigniter中的帖子

I'm using codeigniter and I have to use json encode using jquery and when I want to give the data and validate them I have problem. My code is:

$this->load->library('form_validation');
        $this->form_validation->set_error_delimiters('<div class="nNote nFailure hideit"><p>', '</p></div>');
        $this->form_validation->set_rules('Name', 'Name', 'trim|required|xss_clean');
        $this->form_validation->set_rules('Parent', 'Parent', 'required|xss_clean');
        $this->form_validation->set_rules('Language', 'Language', 'required|xss_clean');
        $this->form_validation->set_rules('Order', 'Order', 'required|xss_clean');
if ($this->form_validation->run())
{
 //do something here
}

How should I handle the post request with JSON?

  • 写回答

1条回答 默认 最新

  • dsf4354353452 2013-12-07 18:51
    关注

    I'm honestly not sure what you're asking, but if you want to respond with json, it's really quite simple, PHP makes this easy for us.

    json_encode(array('jsonKey' => 'jsonValue', 'jsonKey2' => 'jsonValue2'));
    

    Just set the appropriate headers and output that using your favorite mechanism (echo, output buffers, etc)

    EDIT: If you want to go in the reverse (you're being presented with JSON data in your script)

    Just do the following

    $myGreatAssocArray = json_decode($jsonStringData);
    

    Then, you end up with an associate array that you can easily grab data from, as follows:

    $firstJsonValue = $myGretAssocArray['jsonKey1'];
    

    It's really very convenient (and way easier than with say, Java :P )

    Keep in mind though, json_decode is not only going to return arrays. If the JSON is malformed, NULL is going to be returned, so if you need to do any conditionals make sure to use !== NULL or something to that affect.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵