dsshsta97935 2010-10-28 08:52
浏览 91
已采纳

Json在PHP中解码

I am trying to decode JSON in php I sended using an ajax call in javascript (jquery).

The javascript code:

    var editSchedule_data = {
        'action'            : 'editSchedule',
        'eventid'           : $('#eventid').val(),
        'pixels_per_minute' :$('#pixels_per_minute').val(),
         artists: {
            artist: []
        }
    }

    $('.artist').each(function(index) {
        var id=$(this).attr('id');
        id=id.split('_');
        editSchedule_data.artists.artist.push({
            'artistid'    : id[0],
            'stageid'     : id[1],
            'left'        : $(this).offset().left-$('.artists').offset().left,
            'width'       : $(this).width()
        });
    });

    $.ajax({
        type :   "POST",
        url  :   "callback.php",
        data :   editSchedule_data,
        dataType :   "json",
        async    :   true,
        success   :   function(json)  {
            if(json.success)  {
                showSucces(json.message);

            }
            else{
                showError(json.message);
            }
        },
        error: function(error){
            alert("An error occurred: "+error.message);
        }
    });

The php-code:

$clean = sanitize($_POST);

echo(json_encode($clean['artists']),
json_last_error());

echo(json_decode($clean['artists']),
json_last_error());

My output: encode:

{"artist":[{"artistid":"4","stageid":"3","left":"360","width":"240"},{"artistid":"3","stageid":"4","left":"120","width":"240"},{"artistid":"1","stageid":"5","left":"120","width":"180"},{"artistid":"2","stageid":"5","left":"300","width":"120"},{"artistid":"5","stageid":"6","left":"480","width":"120"}]}
0

decode:

0

Can someone tell me how to get the decode function to work?

  • 写回答

3条回答 默认 最新

  • douwen7603 2010-10-28 11:24
    关注

    Why are you trying to use json_decode there? You already have the data as array in $_POST.That's why json_decode fails and retuns NULL...it expects a properly formated string and you are passing it an array. If the dataType : "json" parameter confuses you, it specifies the type of the data you are expecting back from the server not the type of the data you are sending. You shoud simply process the data from $_POST, create your response , apply json_encode on it and echo the resulted string.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)