0

我对 ActionScript (AS3) / JSON 有疑问。对此可能有一个非常简单的答案,所以如果这似乎是一个非常菜鸟的问题,我深表歉意。

我要做的是获取多个文本字符串,将它们放入一个对象中,然后将该对象作为 JSON 字符串发送到服务器。

我的代码中的其他所有内容都可以正常工作,每当我在文本输入框中输入一段文本并发送对象时,该数组都会产生如下所示的空条目:

{"email":null,"last_Name":null,"first_Name":null,"date_of_birth":null,"telephone":null}

但是,如果我替换customer.first_name = inPutfname为:customer.first_name = " John";

然后数组追溯如下,这就是我想要的:

{"email":null,"last_Name":null,"first_Name":John,"date_of_birth":null,"telephone":null} 

那么如何获取多个输入文本字符串并将它们放入对象中,以便数组像上面示例中那样读回,或者理想情况下像这个示例一样:

{"email":randomemail@email.com"last_Name":smith,"first_Name":john,"date_of_birth":27/07/1989,"telephone":012343456788}

这是我到目前为止的代码:

var inPutFirstname: String;

var inPutLastname: String;

var inPutEmail: String;

var inPutTelephone: String;

var inPutDob: String;


// changes customer data into an object
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

var customer:Object = new Object;

customer.first_Name =inPutFirstname;

customer.last_Name =  inPutLastname;           PROBLEM AREA?

customer.email = inPutEmail;

customer.date_of_birth = inPutDob;

customer.telephone = inPutTelephone;
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\


//changes customer object into json string

var myJson: String = JSON.stringify(customer);


var myVariableUrl: URLVariables = new URLVariables();
myVariableUrl.data = JSON.stringify(myVariableUrl);

var myRequestUrl: URLRequest = new URLRequest();
var authHeaderUrl: URLRequestHeader = new URLRequestHeader("xxxxxx", "xxxxxxx");

myRequestUrl.method = URLRequestMethod.POST;
myRequestUrl.data = myJson;
myRequestUrl.url = "website url"

var uload: URLLoader = new URLLoader();
uload.load(myRequestUrl);

uload.load(myRequestUrl);





mySendbutton.addEventListener(MouseEvent.MOUSE_DOWN, thefunction1);
// captures text from the input and places them into the customer object
function captureText1(): void {

inPutFirstname = myFirstName.text;
inPutLastname = myLastName.text;
inPutEmail = myEmail.text;
inPutDob = myDob1.text + myDob2.text + myDob3.text;
inPutTelephone = myTele.text;

}

 function thefunction1(event: MouseEvent): void {
 captureText1();
 trace(inPutfname);
 trace(myJson);
}

任何帮助都会很棒。

多谢你们

4

2 回答 2

0

首先,您可以添加函数 captureText1() 跟踪:

trace(myFirstName.text);
trace(myLastName.text);
trace(myEmail.text);
etc...

只是为了确保你在那里有适当的价值。如果你是,而不是使用额外的变量,你可以这样做(在跟踪之后):

var customer:Object = new Object;
customer.first_Name = myFirstName.text;
customer.last_Name =  myLastName.text;
customer.email = myEmail.text;
customer.date_of_birth = "" + myDob1.text + myDob2.text + myDob3.text;
customer.telephone = myTele.text;

var myJson: String = JSON.stringify(customer);
trace(myJson);
于 2014-01-16T00:40:06.953 回答
0

您的问题是创建 JSON 对象的代码似乎没有包含在函数中,这意味着它将立即执行(考虑到这可能是附加到框架的代码)。

您需要做的是将与创建 JSON 对象相关的代码包装到一个函数中,如下所示:

(请注意,有很多方法可以更好地构建此代码,但这应该说明一些核心概念。)

var inPutFirstname: String;
var inPutLastname: String;
var inPutEmail: String;
var inPutTelephone: String;
var inPutDob: String;

function generateJSON():String {
    var customer:Object = new Object();

    customer.first_Name = inPutFirstname;
    customer.last_Name = inPutLastname;
    customer.email = inPutEmail;
    customer.date_of_birth = inPutDob;
    customer.telephone = inPutTelephone;

    return JSON.stringify(customer);
}

function sendJSON(data)
    var myVariableUrl: URLVariables = new URLVariables();
    myVariableUrl.data = JSON.stringify(myVariableUrl);

    var myRequestUrl: URLRequest = new URLRequest();
    var authHeaderUrl: URLRequestHeader = new URLRequestHeader("xxxxxx", "xxxxxxx");

    myRequestUrl.method = URLRequestMethod.POST;
    myRequestUrl.data = data;
    myRequestUrl.url = "website url"

    var uload: URLLoader = new URLLoader();
    uload.load(myRequestUrl);
}

mySendbutton.addEventListener(MouseEvent.MOUSE_DOWN, thefunction1);

// captures text from the input and places them into the customer object
function captureText1(): void {
    inPutFirstname = myFirstName.text;
    inPutLastname = myLastName.text;
    inPutEmail = myEmail.text;
    inPutDob = myDob1.text + myDob2.text + myDob3.text;
    inPutTelephone = myTele.text;
}

function thefunction1(event: MouseEvent): void {
    // First populate the string variables with user input
    captureText1();

    // Then generate the JSON string
    var jsonData:String = generateJSON();

    // Lastly, send the JSON string
    sendJSON(jsonData);

    trace(inPutfname);
    trace(jsonData);
}
于 2014-01-16T00:33:24.130 回答