emjay050410 / as3corelib

Automatically exported from code.google.com/p/as3corelib
1 stars 0 forks source link

JSON.decode() can't be converted into an Array #100

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
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","Cod
Res":"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":"A1
5","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"},{"RefE
lt":"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"},{"RefE
lt":"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"},{"RefEl
t":"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","TypRe
s":"B","Act":"H"},{"RefElt":"156\/1","CodRes":"N3","TypRes":"B","Act":"H"},{"Ref
Elt":"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...

Original issue reported on code.google.com by jon_le_k...@hotmail.com on 17 Mar 2009 at 10:39

GoogleCodeExporter commented 8 years ago
PS : I'm using as3corelib-.92.1 with Flex Builder 3

Original comment by jon_le_k...@hotmail.com on 17 Mar 2009 at 10:44

GoogleCodeExporter commented 8 years ago
Have you tried using  var Test:Array = ArrayUtil.toArray(obj);
That would be my suggested solution...

Original comment by NielsBjerg on 31 Mar 2009 at 11:12

GoogleCodeExporter commented 8 years ago
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 ;)

Original comment by jon_le_k...@hotmail.com on 7 Apr 2009 at 8:26

GoogleCodeExporter commented 8 years ago

Original comment by darron.schall on 8 Jul 2009 at 7:02