0

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

4
  • JSON.parse('[{"AA":"A1", "BB":"32"}, {"AA":"A2", "BB":"12"}, {"AA":"A3", "BB":"14"}]') works well in JS console Commented Oct 3, 2013 at 21:09
  • Can you tell use the error that you are getting? I would say, since this is a list, add a wrapper around the list so after you parse the json you can call propertySets.listOfObjects Commented Oct 3, 2013 at 21:12
  • My bad. This works fine all of a sudden. Apologies for the false call; there was something wrong with the project itself in FlashBuilder. Thank you for the help with the JS console bit. Commented Oct 3, 2013 at 22:46
  • You should close your question as SO thrives to keep everything answered :) Commented Jun 4, 2014 at 7:59

2 Answers 2

1

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

Comments

0

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"}]}';

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.