简体   繁体   English

JSON Stringify-PHP json_decode

[英]JSON Stringify - PHP json_decode

I am new to JSON,ofr a project of my own I need to send array of objects to the server. 我是JSON的新手,这是我自己的项目,我需要将对象数组发送到服务器。 Basically, I have a Question object, which has a couple of properties, like question, optionA, optionB etc. I have an array of Question objects. 基本上,我有一个Question对象,它具有几个属性,例如问题,optionA,optionB等。我有一系列Question对象。

example: 例:

qArray = new Array();
qArray.push(new Question("where do you live?", "England", "ıtaly", "Usa");

I am adding a lot of questions objects to this array and in the end, I need to send this array to the server, like this: 我向该数组添加了很多问题对象,最后,我需要将该数组发送到服务器,如下所示:

$.post("backend-stuff/aj-save-test.php", { testName : $("#testName").val().toString(), 'questions' : JSON.stringify(qArray)}, function(result){
        alert(result);
    });
}

In PHP, I use this, 在PHP中,我使用这个

$questions = json_decode($_POST["questions"]);
$testName = $_POST["testName"];

Problem is that I cant read data, either I am sending it in the wrong way or whats wrong is on the php side. 问题是我无法读取数据,或者我以错误的方式发送数据,或者PHP方面出了什么问题。

Thank you for answers, I checked Google before sending the question, so please forgive me if this is so easy but for me its not so. 谢谢您的回答,我在发送问题之前已经检查过Google,因此,如果这很容易,请原谅,但对我而言并非如此。

要将stdClassObject转换为数组,请检出get_object_vars()

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

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