views:

122

answers:

1

Hi Stackers, so I have a JSON link that contains a couple of nodes (not sure what you call them in JSON) that I need to put into arrays in ActionScript, but I'm still having trouble trying to trace out all the specific node content.

I found a similar question here, but the fix just showed how to trace out the entire JSON file (which shows up in my output window as [object Object],[object Object],[object Object],...)

first node: {"captions":[{"content":"[hello world] ","startTime":0,"duration":4000}

My code:

private function onCaptionsComplete( event:Event ):void
    {
        //var jsonObj:Object = JSON.decode(event.target.data);
        //var englishCaptionsObject = jsonObj;
        var englishCaptionsObject:Object = JSON.decode(cc_loader.data);

        captionsContent.push(englishCaptionsObject);
        trace("captionsContent = "+captionsContent);

        for (var i:String in englishCaptionsObject)
        {
            trace(i + ": " +englishCaptionsObject[i]);
            trace(i + ": " +englishCaptionsObject[i].content);
            trace(i + ": " +englishCaptionsObject[i].content[i]);
            trace(i + ": " +englishCaptionsObject[i].startTime[i]);
        }
    }

When I run it here are my traces below:

captionsContent = [object Object]
captions: [object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
captions: undefined
TypeError: Error #1010: A term is undefined and has no properties.

What I would like to be able to do is put the content data into the captionsContent Array, and the startingTime into a startingTime Array, same with the duration data.

Any tips?

+2  A: 

You have an error in your loop you use 3 times the variable i to access three differents Object (englishCaptionsObject, content and startTime) :

Edit : i have assumed that your first decode was already good, but that is not the case, Your captions Array is in the captions field of your json data, content and startTime are not Arrays :

// get the captions array
var captions:Array=englishCaptionsObject.captions;

var cnt:int=0;

// loop throug each caption
for each (var caption:Object in captions) {
     var content:String = caption.content;
     trace(cnt+" "+content);

     var startTime:Number = Number(caption.startTime);
     trace(cnt+" "+startTime);

     cnt++;
}
Patrick
In the first trace I get [object Object],[object Object],... the 2nd trace I get null and the 3rd throws an error :( TypeError: Error #1009: Cannot access a property or method of a null object reference. Do you know if there is a way to see the actual string content in the Flash output window?
Leon
@leon i have modified the answer you were not getting the right field
Patrick
ah sweet that was it! Thanks :)
Leon