简体   繁体   English

Json在PHP中解码

[英]Json decoding in PHP

I am trying to decode JSON in php I sended using an ajax call in javascript (jquery). 我试图解码我在javascript(jquery)中使用ajax调用发送的php中的JSON。

The javascript code: javascript代码:

    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: php代码:

$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? 有人能告诉我如何使解码功能工作?

Why are you trying to use json_decode there? 你为什么要在那里使用json_decode? 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. 你已经在$ _POST中将数据作为数组。这就是为什么json_decode失败并返回NULL ...它期望一个正确格式化的字符串,你传递一个数组。 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. 如果dataType:“json”参数让您感到困惑,它会指定您希望从服务器返回的数据类型,而不是您要发送的数据类型。 You shoud simply process the data from $_POST, create your response , apply json_encode on it and echo the resulted string. 您只需处理$ _POST中的数据,创建响应,在其上应用json_encode并回显结果字符串。

json_decode($clean['artists']); is giving you an object, that's why echoing it doesn't show anything. 给你一个对象,这就是回声它没有显示任何东西的原因。 Try print_r() to see what's inside the object: 尝试使用print_r()查看对象内部的内容:

// Will show you the whole PHP object
print_r( json_decode( $clean['artists'] ) ); 

it is much better if you will use this. 如果你使用它会好得多。 when fetching data from the database 从数据库中获取数据时

$.getJSON("callback.php",function(json){
$('#div to update').html(json[0].databasefield); // 0 if query is only 1 result
});                                 // when there are many of them use for loop 

on your php you should encode it using json_encode(array values); 在你的PHP上你应该使用json_encode(数组值)编码它;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM