douyin4875 2009-07-16 19:59
浏览 104
已采纳

为jQuery日历编码JSON

I'm trying to use the PHP json_encode function to encode some JSON to send along to a jQuery plugin that will render out a calendar. The plugin's name is FullCalendar.

I've started on grabbing event data from a MySQL database and encoding it in a JSON string, but I've run into a problem. The default JSON example that comes with the plugin works just fine, but mine does not.

Here is the example code - link | Here is the output - link

Here is my code - link | Here is the output - link

If you look at the outputs, there are suttle differences, but I believe that these differences are what is making it not work. You'll see that in the example output, there are brackets [] around everything, and containing each individual JSON string is just the curly braces {}. In my output, there are just curly braces {} containing each string, no brackets on the outside.

Help please? This is my first time using JSON!

  • 写回答

1条回答 默认 最新

  • dongpengqin3898 2009-07-16 20:08
    关注

    You're echoing each string separately, so json_encode never knows it's a list.

    You could change your while statement to build up the list:

    while($row = $result->fetch_array(MYSQLI_ASSOC))
    {
        echo json_encode(array( 
        ....
    

    to

    $rows = array();
    while($row = $result->fetch_array(MYSQLI_ASSOC))
    {
         $rows[] = array( 
         ....
    }
    

    then json_encode the whole thing:

    echo json_encode($rows);
    

    That will output the [{...},{...}] structure you're looking for, which is a valid JSON object.

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

报告相同问题?

悬赏问题

  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)