0

I am trying to send JSON from an actionscript/flex android app via POST. But I can't figure out how to create a JSON object in the right way.

it should look like this:

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

my code:

var request: URLRequest = new URLRequest(url);

// How do I create the data?

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

I have tried sending it as String, but then the server receives it with "" around, and can't parse it as JSON. I have also tried creating it as URLVariables, but then I don't know how to make it nested.

2 Answers 2

2

If you are targeting FlashPlayer-version 11 you can use the built in JSON-parser:

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
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the answer. I solved it with writing it as a String. The main problem was figuring out how to format the string.
1

Finally figured it out. It was a matter of formatting the brackets the right way.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.