如何在actionscript中创建和发送json对象?

时间:2013-11-05 07:50:33

标签: json flex actionscript

我试图通过POST从actionscript / flex android应用程序发送JSON。但我无法弄清楚如何以正确的方式创建JSON对象。

它应该是这样的:

"pos"=>
{
"x"=>234,
"y"=>234
},
"gps"=>
{
"latitude"=>52.123,
"longitude"=>11.123
},
"event"=>"participation"

我的代码:

var request: URLRequest = new URLRequest(url);

// How do I create the data?

request.data = data;
request.contentType = "application/json";
request.method = URLRequestMethod.POST;

我尝试将其作为String发送,但随后服务器用“”接收它,并且无法将其解析为JSON。我也尝试将其创建为URLVariables,但后来我不知道如何使其嵌套。

2 个答案:

答案 0 :(得分:2)

如果您的目标是FlashPlayer-version 11,则可以使用内置的JSON解析器:

var o:Object = {
    pos: { x:234, y:234 },
    gps: { latitude: 52.123, longitude:11.123 },
    event: "participation"
};

var s:String = JSON.stringify(o);
trace(s); //outputs - {"gps":{"latitude":52.123,"longitude":11.123},"pos":{"y":234,"x":234},"event":"participation"}

var o2:Object = JSON.parse(s);
trace(o2["event"]); //outputs - participation

答案 1 :(得分:1)

终于明白了。这是以正确方式格式化括号的问题。

var data:String = '{"pos":{"x":234, "y":234}, "gps":{"latitude":52.123, "longitude":11.123}, "event":"participation"}';