as3corelib
as3corelib copied to clipboard
JSON.decode() can't be converted into an Array
Originally filed by [email protected] on 2009-03-17T10:39:43
What steps will reproduce the problem?
- First Step : Creation of the JSON File I use PHP and json_encode() function to encode an array into JSON The result of this encoding is : {"NbElt":40,"LstElt":[{"RefElt":"139/1","CodRes":"A1","TypRes":"B","Act":"H"},{"RefElt":"162/1","CodRes":"A10","TypRes":"B","Act":"H"},{"RefElt":"167/1","CodRes":"A11","TypRes":"B","Act":"H"},{"RefElt":"164/1","CodRes":"A12","TypRes":"B","Act":"H"},{"RefElt":"169/1","CodRes":"A13","TypRes":"B","Act":"H"},{"RefElt":"173/1","CodRes":"A14","TypRes":"B","Act":"H"},{"RefElt":"174/1","CodRes":"A15","TypRes":"B","Act":"H"},{"RefElt":"175/1","CodRes":"A16","TypRes":"B","Act":"H"},{"RefElt":"176/1","CodRes":"A17","TypRes":"B","Act":"H"},{"RefElt":"144/1","CodRes":"A2","TypRes":"B","Act":"H"},{"RefElt":"145/1","CodRes":"A3","TypRes":"B","Act":"H"},{"RefElt":"146/1","CodRes":"A4","TypRes":"B","Act":"H"},{"RefElt":"147/1","CodRes":"A5","TypRes":"B","Act":"H"},{"RefElt":"159/1","CodRes":"A6","TypRes":"B","Act":"H"},{"RefElt":"160/1","CodRes":"A7","TypRes":"B","Act":"H"},{"RefElt":"148/1","CodRes":"A8","TypRes":"B","Act":"H"},{"RefElt":"161/1","CodRes":"A9","TypRes":"B","Act":"H"},{"RefElt":"170/1","CodRes":"ATT","TypRes":"B","Act":"H"},{"RefElt":"149/1","CodRes":"B1","TypRes":"B","Act":"H"},{"RefElt":"150/1","CodRes":"B2","TypRes":"B","Act":"H"},{"RefElt":"151/1","CodRes":"B3","TypRes":"B","Act":"H"},{"RefElt":"152/1","CodRes":"B4","TypRes":"B","Act":"H"},{"RefElt":"153/1","CodRes":"B5","TypRes":"B","Act":"H"},{"RefElt":"168/1","CodRes":"B6","TypRes":"B","Act":"H"},{"RefElt":"177/1","CodRes":"B7","TypRes":"B","Act":"H"},{"RefElt":"188/1","CodRes":"B8","TypRes":"B","Act":"H"},{"RefElt":"171/1","CodRes":"LCA","TypRes":"B","Act":"H"},{"RefElt":"163/3","CodRes":"M1","TypRes":"B","Act":"H"},{"RefElt":"172/1","CodRes":"M90","TypRes":"F","Act":"H"},{"RefElt":"166/1","CodRes":"M99","TypRes":"F","Act":"H"},{"RefElt":"154/1","CodRes":"N1","TypRes":"B","Act":"H"},{"RefElt":"155/1","CodRes":"N2","TypRes":"B","Act":"H"},{"RefElt":"156/1","CodRes":"N3","TypRes":"B","Act":"H"},{"RefElt":"157/1","CodRes":"N4","TypRes":"B","Act":"H"},{"RefElt":"158/1","CodRes":"N5","TypRes":"B","Act":"H"},{"RefElt":"142/1","CodRes":"R1","TypRes":"B","Act":"H"},{"RefElt":"141/1","CodRes":"R2","TypRes":"B","Act":"H"},{"RefElt":"143/1","CodRes":"R3","TypRes":"B","Act":"H"},{"RefElt":"140/1","CodRes":"R4","TypRes":"B","Act":"H"},{"RefElt":"165/3","CodRes":"S1","TypRes":"B","Act":"H"}]} And this json is validated by http://www.jsonlint.com/
- Second Step : Loading JSON File in Flex
mx:Script <![CDATA[ import mx.controls.Alert; import com.adobe.serialization.json.JSON; var Loader : URLLoader = new URLLoader(); //Function : Loading Resource list
public function loadresources() : void
{
Loader.addEventListener(Event.COMPLETE, ResourceCompleteHandlder);
Loader.load(new URLRequest("http://127.0.0.1/test.php"));
}
public function ResourceCompleteHandlder(event:Event) : void
{
var obj : Object = JSON.decode(Loader.data); // Here, the object seems
to be created
Alert.show("The Object -> " + obj.toString());
var Test : Array = (obj as Array); // But here, I can't convert it
to an Array, Test == null
Alert.show("Array Size -> " + Test.length.toString());
}
]]>
/mx:Script
The main problem is that I can't convert the PHP JSON into an ActionScript Array...
Updated by [email protected] on 2009-03-17T10:44:07
PS : I'm using as3corelib-.92.1 with Flex Builder 3
Updated by NielsBjerg on 2009-03-31T11:12:39
Have you tried using var Test:Array = ArrayUtil.toArray(obj); That would be my suggested solution...
Updated by [email protected] on 2009-04-07T08:26:46
Thank you NielsBjerg, I've tried it and your solution works. I think it may be better than using the object immediatly like I'm doing now, thats why I will change it next time ;)
Thank you again for your solution ;)
Updated by darron.schall on 2009-07-08T19:02:50
Original ticket set status to NotABug (we converted to open)