user2789284
user2789284

Reputation: 771

Parse json string that carries a collection of objects in actionscript using Json.parse

I am running into an error parsing a json string with JSON.parse(...)

var str:String= '[{"AA":"A1", "BB":"32"}, {"AA":"A2", "BB":"12"}, {"AA":"A3", "BB":"14"}]';
var propertySets:Object = JSON.parse(str);

I can tell that I am getting the syntax wrong with constructing the JSON string but I have tried quite a few things before giving up. Any help with how to deal with collections would be great.

The following simple case works for me

var str:String= '{"test":"line1"}';
var propertySets:Object = JSON.parse(str);

Thank you

Upvotes: 0

Views: 513

Answers (2)

greglieb
greglieb

Reputation: 64

The syntax in the JSON string is correct. The way it is formatted will return an Array instance from JSON.parse(). The following code works for me:

var str:String = '[{"AA":"A1", "BB":"32"}, {"AA":"A2", "BB":"12"}, {"AA":"A3", "BB":"14"}]';
var propertySets:Array = JSON.parse(str) as Array;

trace(propertySets[0].AA); // prints "A1"
trace(propertySets[0].BB); // prints "32"

Upvotes: 1

user2033671
user2033671

Reputation:

Shot in the dark since I don't know actionscript, but try wrapping the array in an object

var str:String= '{"objectArray":[{"AA":"A1", "BB":"32"}, {"AA":"A2", "BB":"12"}, {"AA":"A3","BB":"14"}]}';

Upvotes: 0

Related Questions