ActionScript(AS3)对象/ JSON文本输入

时间:2014-01-15 20:39:13

标签: arrays json actionscript-3 jsonobject

我遇到了ActionScript(AS3)/ JSON的问题。对此可能有一个非常简单的答案,所以如果这看起来像一个真正的noob问题我会道歉。

我要做的是获取多个文本字符串,将它们放入一个对象中,然后将该对象作为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);
}

任何帮助都会很棒。

谢谢你们

2 个答案:

答案 0 :(得分: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);
}

答案 1 :(得分: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);