使用PHP从JSON中检索对象


Retrieving objects from JSON using PHP

我需要将存储在IndexedDB中的一些数据发送到服务器,以便进行一些后端操作。使用JSON.stringify()将所需的数据提取到javascript中的变量payLoad中。

 payLoad = "[{"synch":0,"id":-1,"name":"Tester","email":"test@example.com","created":"2014-08-20T07:56:44.201Z"}]";
    $.ajax({
                type: "POST",
                url: "process.php",
                data: payLoad,      // NOTE CHANGE HERE
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function(msg) {
                    alert(msg);
                },
                error: function(msg) {
                alert('error');
                }
            });

我可以将这个JSON数据解析为PHP类吗?

这样,您只需在正文中发送JSON原始。试试这个:

$data = json_decode(file_get_contents('php://input'));

另一方面,如果您发送的数据是这样的:

data: { data: payLoad },

然后你可以简单地进行

$data = json_decode($_POST['data']);